Function parameters that include quotes

This is a discussion on "Function parameters that include quotes" within the JavaScript Forum section. This forum, and the thread "Function parameters that include quotes are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Sep 15th, 2006, 00:47
New Member
Join Date: Sep 2006
Location: london
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Function parameters that include quotes

hi there,

I'm dynamically constructing(PHP) an img element with an onClick event-handler which calls a JavaScript function.
Code: Select all
$output_txt .= "<td><img src='./usr_img/tb_".$row['img_id'];
$output_txt .= ".jpg' onClick='changeImg(".$row["img_id"].", \"".$row["img_title"]."\", \"";
$output_txt .= $row["img_subtitle"] . "\");'";
my JS function:
Code: Select all
function changeImg(imgID, title, subtitle)
{
    document.getElementById('mainImg').style.backgroundImage = "url('./usr_img/" + imgID + ".jpg')";
    document.getElementById('mainTitle').firstChild.nodeValue = "";
    document.getElementById('mainTitle').firstChild.nodeValue += title; 
    document.getElementById('mainSubtitle').firstChild.nodeValue = "";
    document.getElementById('mainSubtitle').firstChild.nodeValue += subtitle;    
}
A major problem arises whenever the title or subtitle vars include quotes.
I use the htmlspecialchars php function to change the quotes to their html entities, which i've verified in the page source,
but it seems the JS function converts them back to regular characters and then dies upon finding a quote.

any help would be greatly appreciated.

Last edited by loorp; Sep 15th, 2006 at 08:26.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Sep 15th, 2006, 02:03
Ryan Fait's Avatar
Elite Veteran
Join Date: May 2006
Location: Las Vegas
Posts: 3,787
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Function parameters that include quotes

You can escape quotes in both PHP and javascript:

Code: Select all
var html = "<img src=\"logo.jpg\" width=\"120\" height=\"40\" alt=\"Company X\" />";
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Sep 15th, 2006, 02:04
Ryan Fait's Avatar
Elite Veteran
Join Date: May 2006
Location: Las Vegas
Posts: 3,787
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Function parameters that include quotes

Oh, and if you've already tried that, attempt to use double slashes somewhere in there.

Code: Select all
"<img src=\\"logo.jpg\\" width=\\"120\\" height=\\"40\\" alt=\\"Company X\\" />"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Sep 15th, 2006, 07:53
New Member
Join Date: Sep 2006
Location: london
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Function parameters that include quotes

As you can see from the code, I am already escaping one set of quotes.
at the moment, by using,
Code: Select all
\"
I am able to pass text through that contain single quotes.
Strings containing double quotes still die.

if I change it to
Code: Select all
\'
The I am able to pass through strings containing double quotes but single quotes in the string still kills the function.

The question is how to have a javascript function accept an HTML entity in the parameter and not convert it to it's corresponding character.

Here is my JavaScript function call in the resultant HTML page
Code: Select all
onClick='changeImg(138, "There's some goin&quot;down", "HOwZ&amp;at");'
and here is the JS error

Quote:
Error: missing ) after argument list
Source File: ./site/img_gallery.php?page=2
Line: 1, Column: 39
Source Code:
changeImg(138, "There's some goin"down", "HOwZ&at");

Last edited by loorp; Sep 15th, 2006 at 07:58.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Sep 15th, 2006, 18:02
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Function parameters that include quotes

onClick='changeImg(138, "There's some goin&quot;down", "HOwZ&amp;at");'

Look at the bit I've highlighted. The single quotes are seen as matching and what's between them is taken as your function so the error message is quite right.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Sep 16th, 2006, 10:01
New Member
Join Date: Sep 2006
Location: london
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Function parameters that include quotes

thanks for the reply.

Yes the error message is totally understandable.

I would however like to resolve this.
I've tried addslahes in PHP to slash out that first ' but then the resulting html test is
There\'s some...

Is there a "stripslashes" function available in JS?

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Sep 16th, 2006, 10:51
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Function parameters that include quotes

Try this format:
Code: Select all
onClick="changeImg(138, 'There''s some goin down', 'HOwZ&amp;at');"
NOTE: That's two single quotes between the e and s in There''s and I have taken oout the &quot;.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Sep 17th, 2006, 04:18
Ryan Fait's Avatar
Elite Veteran
Join Date: May 2006
Location: Las Vegas
Posts: 3,787
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Function parameters that include quotes

Yes, stripslashes() is a PHP function.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
function, parameters, include, quotes

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Include PHP in MYSQL Function? Jack Franklin PHP Forum 3 Mar 24th, 2008 19:35
Problem with displaying random quotes from a function Z101 JavaScript Forum 3 Dec 7th, 2007 22:34
[SOLVED] Does using PHP include() function affect my SEO ? RohanShenoy Search Engine Optimization (SEO) 3 Nov 22nd, 2007 18:13
Quotes in strings jakyra Classic ASP 5 Sep 9th, 2003 15:10


All times are GMT. The time now is 21:39.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42