[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

innerHTML replaces & with &

Posted on 2007-07-26
10
Medium Priority
?
1,030 Views
Last Modified: 2012-08-13
I have an ajax script that returns a lot of data including an image tag
<img src="index.php?module=image&action=resize&pic=temp.jpg"/>

Now when I want to place the data into a div tag
var response = transport.responseText || "error while retrieving tab "+file;
document.getElementById('tabContent').innerHTML = response;
                      
innerHTML replaces & with &amp;
needless to say my image source link won't work of course. How can I stop innerHTML from replacing &?
0
Comment
Question by:zotteke123
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 19578566
How are you checking that? That would probably not cause any problem.

Can you browse to index.php?module=image&action=resize&pic=temp.jpg and get the image manually? Make sure to use the same exact path. For example if your URL to the page where you are using AJAX is

http://someplace.com/somedir/somepage.php

then use

http://someplace.com/somedir/index.php?module=image&action=resize&pic=temp.jpg

does the above URL work? If your index.php is in root directory then the image tag should probably be

<img src="index.php?module=image&action=resize&pic=temp.jpg"/>
0
 
LVL 11

Expert Comment

by:raj3060
ID: 19578567
change & with &amp;

<img src="index.php?module=image&amp;action=resize&amp;pic=temp.jpg"/>
0
 
LVL 58

Expert Comment

by:amit_g
ID: 19578570
the last line should be

<img src="/index.php?module=image&action=resize&pic=temp.jpg"/>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:DigitalTyrant
ID: 19578580
I have used Ajax calls on multiple projects to load HTML into an object and haven't ever experienced this problem.  Would you mind posting you javascript source?
0
 
LVL 48

Expert Comment

by:hernst42
ID: 19578636
Maybe the problem is more on the server-part which escapes multiple times the & in the response. Which AJAX framework are you using?
0
 

Author Comment

by:zotteke123
ID: 19578662
simple testcase:
<div id="data">test</div>
<script>
document.getElementById('data').innerHTML = '<img src="index.php?module=image&action=resize&pic=temp.jpg"/>';
alert(document.getElementById('data').innerHTML);
</script>

innerHTML will replace & with &amp;
which makes the url useless...
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 600 total points
ID: 19578751
That example is correct. It should display that way, but the browser should request it without the & when asking the server.
Request send to the server:
[27/Jul/2007:00:04:11 +0200] "GET index.php?module=image&action=resize&pic=temp.jpg HTTP/1.1" 200 133  "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.5) Gecko/20070713 Firefox/2.0.0.5"
So the everything is valid. See http://www.htmlhelp.com/tools/validator/problems.html#amp
0
 
LVL 58

Assisted Solution

by:amit_g
amit_g earned 450 total points
ID: 19578801
Your example works just fine. I have used this several times and it works without any problem. You are looking at the wrong place. Check index.php manually in the browser. Most likely you are using incorrect path. The link has to be either absolute or relative to current URL.

Another possibility is that server itself is sending incorrect data.
0
 
LVL 4

Assisted Solution

by:DigitalTyrant
DigitalTyrant earned 450 total points
ID: 19578821
The alert() function will automatically encode & as &amp;  This doesn't mean the html is being encoded as well.  If you check the properties of the image, does it show &amp; or just &?
0
 

Author Comment

by:zotteke123
ID: 19581138
I was being an idiot
Must be tired
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question