document.write outputing "&" as & in URL
So i am having more trouble with Javascript!
Everything was working great until i made a slight change to my script - just added a new function and assigned the value to a variable that was before static.
i am using document.write to print out a tag with a dynamic url inside for the source.
The only problem is for some reason it is printing the & in the URL as &
and becuase of this my include will not work (its a JSON external URL and im using the callback function).
For example it do:
document.write('<scr' + 'ipt src="http://jsonsource.com/index.php?j=t&y=r&h=4" type="text/javascript"><\/scr' + 'ipt>');
but it is printing the "&" as
&
It is stopping the JSON from working?
I even took the actual value of the URL and inserted it as innerHTML to another element on the page and it was using the &.
Can anyone advise me how i can get javascript to print it as & and not &
would be really greatful of any help.
greg posted this at 01:36 — 27th March 2009.
He has: 1,581 posts
Joined: Nov 2005
Have you tried using the entity?
&
document.write('<scr' + 'ipt src="http://jsonsource.com/index.php?j=t&y=r&h=4" type="text/javascript"><\/scr' + 'ipt>');
It's common practice anyway...
decibel.places posted this at 03:42 — 27th March 2009.
He has: 1,494 posts
Joined: Jun 2008
or you could try escaping and unescaping the string: "&" = "%26"
btw - document.write will not work in an XHTML doc
benf posted this at 07:43 — 27th March 2009.
They have: 426 posts
Joined: Feb 2005
Yes i have tried all of these options, escaping and using
&
and none appear to work. Only when i use the & does it work.i did previously createElement and appendChild to insert the tag on the page. However, the callback function did not work. Only when i document.write did it work.
Any ideas on this?
Good Value Professional VPS Hosting
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.