User avatar
Montclair
Posts: 35
Joined: Thu Jun 05, 2008 3:28 pm

cacheAjax set to true causes inline javascript to run twice

Setting cacheAjax to true is causing inline javascript on a page to execute twice.

Consider:

in highslide.js

Code: Select all

preserveContent : false, // Preserve changes made to the content and position of HTML popups.
cacheAjax : true, // Cache ajax popups for instant display. Can be overridden for each popup.
example.html

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="/highslide.css">
<script src="/highslide.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript"> <!--
	alert('You should only see this once, but you are going to see it twice.');
// --></script>
<a href="/a_faq_for_example.html#q1" onclick="return hs.htmlExpand(this,{src:'/faq.shtml#Q1a',objectType:'ajax',align:'center',minWidth:400},{headerText:'Answer to FAQ.'})" title="Read the answer to a FAQ.">Click me for the ajax loaded answer from a large FAQ.</a>
</body>
</html>
The call stack shows hs.Ajax.getElementContent followed by hs.Ajax.loadHTML called directly before the inline script runs the second time.

Conversely, setting cacheAjax to false causes inline javascript to run every time a highslide link is clicked. Change it to false in the code above, and reload. Click the link and you'll get the javascript alert every time.

Any fix?
torstein.honsi
Site Admin
Posts: 9215
Joined: Thu Nov 09, 2006 11:22 am
Location: Vik i Sogn, Norway

Re: cacheAjax set to true causes inline javascript to run twice

I can't reproduce what you're saying - see http://jsfiddle.net/jkzcz/.

In fact, I wonder if your alert should be inside the onclick attribute? That would explain why you would see it twice. First Highslide clicks the onclick virtually to index the popup. If you want to run code on click, use one of the Highslide events like hs.Expander.prototype.onInit instead.
Torstein Hønsi
CTO, Founder
Highsoft
User avatar
Montclair
Posts: 35
Joined: Thu Jun 05, 2008 3:28 pm

Re: cacheAjax set to true causes inline javascript to run twice

Thanks for the reply. I found the problem, and the reason it's not reproduce-able on jsfiddle is that the src of the hs link must actually exist, and that source must also contain the inline code. Consider if an hs link's src is a particular element of a page which contains inline javascript, such as:

Code: Select all

<html><body>
<!-- my file name is test.html -->
<script>
alert('I'm some common code that runs inline on every page of the site. you should only see me once.');
</script>
<div id="the_source_element">some words that i want to get into a highslide popup via ajax</div>
</body></html>
Now, on another page, you want to pop up the contents of the div "the_source_element" using highslide.

Code: Select all

<html><body>
<!-- my file name is main.html -->
<script>
alert('I'm some common code that runs inline on every page of the site. you should only see me once.');
</script>
<a href=/test.html#the_source_element" onclick="return hs.htmlExpand(this,{src:'/test.html#the_source_element',objectType:'ajax',cacheAjax:true})">click me</a>
</body></html>
When main.html loads, highslide reads test.html in advance (because cacheAjax is true) and thus, executes test.html's inline javascript. It appeared as if the javascript was executing twice, since main.html also contains that javascript.

In simpler words, how can I grab the contents of an element on a page containing inline javascript, without executing that target page's javascript?
torstein.honsi
Site Admin
Posts: 9215
Joined: Thu Nov 09, 2006 11:22 am
Location: Vik i Sogn, Norway

Re: cacheAjax set to true causes inline javascript to run twice

How about running it with a condition? The script below will only run before Highslide is initiated on that page:

Code: Select all

if (!hs) alert('I'm some common code that runs inline on every page of the site. you should only see me once.');
Torstein Hønsi
CTO, Founder
Highsoft
User avatar
Montclair
Posts: 35
Joined: Thu Jun 05, 2008 3:28 pm

Re: cacheAjax set to true causes inline javascript to run twice

Thank you, but, I don't really have a problem coming up with a method of bypassing the code. There are several simple conditions for which I can test. Finding out the cause, however, wasn't obvious. HS' behavior of executing inline javascript is not what an XMLHttpRequest would do.

Why would you allow inline javascript to execute when you don't execute external file javascript?

The behavior just isn't expected, especially when only grabbing an element of a page. If you're going to execute inline javascript, you should be executing external javascript as well, and you should only be doing any of that on a full page request. Grabbing singular elements of a page shouldn't execute any javascript on that page.

Just my opinion, I guess.
torstein.honsi
Site Admin
Posts: 9215
Joined: Thu Nov 09, 2006 11:22 am
Location: Vik i Sogn, Norway

Re: cacheAjax set to true causes inline javascript to run twice

Yes I see your point. To be honest I was surprised it evaluated that script at all...
Torstein Hønsi
CTO, Founder
Highsoft
montclairguy
Posts: 6
Joined: Sat Jul 14, 2012 12:20 am

Re: cacheAjax set to true causes inline javascript to run tw

I'd like to revist this 7 year old topic, as I never really patched my code, and I'm not sure you patched yours. Your suggestion was to test for hs in the functions and only run them if it isn't present.

Consider this scenario:

1) Two HTML files that can be viewed independently which both contain highslide.js inclusions and inline javascript.
2) File 1's inline javascript gets excuted. User clicks link which causes hs to open an expander sourcing content from an element in file 2.
3) File 2's inline javascript then executes during the ajax call, even though we only want content from an element.
4) If the files were viewed separately, you need the inline javascript to run.

Questions:

1) You recommended testing for hs. Is hs anything before it's called, simply by inclusion of highslide.js, or does a call have to be made for hs to exist?
2) If file 2 is being viewed on it's own and hs is called from that file, wouldn't the inline javascript not run due to the hs test?
MisterNeutron
Posts: 440
Joined: Sun Aug 18, 2013 11:20 am

Re: cacheAjax set to true causes inline javascript to run tw

Just a heads up... Highslide JS is no longer supported. Torstein hasn't responded to any questions about it in several years. It's abandonware. See the Announcement thread at the top of this forum section.

At present, it would appear that the only person doing any Highslide JS support, of any description, is me, and I'm really more of an HTML/CSS/Java person (yes, Java, not Javascript). The subject of this thread is out of my field, so I can't help. Just be aware that you're very unlikely to get any sort of response whatsoever.

In the immortal words of the Reverend in Blazing Saddles, "Son, you're on your own."

Return to “Highslide JS Usage”