/    Sign up×
Community /Pin to ProfileBookmark

Problem with a string that includes ‘&’ (and it’s not an & issue)

I am incorporating a freeware code onto my site I got from another site. Long story short, the code creates a popup of a large image when a thumbnail is clicked on (but not a new window – the popup is a hidden DIV on the same page).

None of that is particularly relevant except that there is a bug that is causing issues. I will note now that I’m not particularly experienced in JS, but I do have a moderate programming background.

This is the HTML code to call the popup:
<a href=”URL to large image” rel=”functionname” title=”caption for image” credit=”another caption”>
<IMG SRC=”thumb URL” title=”normal title tag”/></a>

IE: it’s all called by the href tag.

So where the problem comes in is in the popup. The JS (external file) creates an array for each image on the page of [0](filename)[1](title caption)[2](credit caption)

To obtain this information, the function uses the method:
imageLink.getAttribute(‘title’) or imageLink.getAttribute(‘credit’)

The issue is this: Some of my captions include ampersands (&). This has been tested with ‘&’, ‘&amp;’ ‘&#number’; (I forget the correct number) and a few other codes. It’s not really important what I tried. The important thing is that in IE (at least IE6), if the caption was “Me, Bob & Joe”, what prints is “Me, Bob “. I’ve confirmed that the variable being asked to print is the cutoff one, so the problem isn’t in the printing of the string, but in the retrieval by getAttribute (as far as I can determine). This issue isn’t present in Firefox.

I have further discovered that what is cutoff is only the LAST ‘&’ and everything after. So ‘1&2&3&4’ would print ‘1&2&3’. I have ‘avoided’ the situation by putting &nbsp; at the end of every caption with an ‘&’. This means in Mozilla there’s a space at the end, and in IE6, there isn’t one. So it’s no big deal. But It’s a hassle to put it into the captions and I’d prefer a solution that solves the problem, and doesn’t just avoid it.

Can anyone help? (Appologies if this has been addressed. I looked far and wide and I couldn’t find an answer).

Thanks very much

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@blah1985Apr 17.2006 — In javascript & is a reserved character, much like ' or " for example you would not do this: alert('I'm cool'); because the browser would see the end ' and think the rest is a varable, an then error out instead you would use alert('I'm cool'); which would tell the browser to ignore the next character. So inorder for your program to work you will need to change the me & someone to me & someone. You can do this by using a variable.replace('&','&') or simply renaming
Copy linkTweet thisAlerts:
@TheHYPOauthorApr 17.2006 — That is one of the many things I attempted. I changed the & to & in the caption. It didn't help. And if you are right, it wouldn't explain why only the [I]LAST [/I] & is cutoff. If it was taking it as an end of the string, woudln't it take the first one too?
Copy linkTweet thisAlerts:
@KorApr 17.2006 — in javascript, if special characters are to be included in a string as literal, one must use unicode escapes;

the unicode escape for & is [B]u0026[/B]

See also:

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Unicode

http://www.hot-tips.co.uk/useful/unicode_converter.HTML
Copy linkTweet thisAlerts:
@TheHYPOauthorApr 18.2006 — Unicode is another of the dozens of things I used.

As I said, if it was anything to do with a special character replacing & with something else, wouldn't every & in the string be an issue? But it's only the final & that cuts off (only in IE)

I tried unicode escape. All I got was u0026 in the middle of my caption.
×

Success!

Help @TheHYPO spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...