Scripting: HTML with jQuery.

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Scripting: HTML with jQuery.

Post by TheQwerty »

I'm trying to write up a script using an advanced configuration dialog and I'd really like to include Javascript to make the dialog a bit more interactive and functional. To keep my wits I'd prefer to use the jQuery framework, and link that from Google instead of requiring an additional file or embedding it.

However, I'm running into problems getting the page to work in XY, and I'm not sure if it's a bug in XY, something about my IE settings, or just jQuery being not supported in the browser component.

A short example script:

Code: Select all

"HTML"
	$html = <<<HEREDOC
<HTML><BODY>
	<SCRIPT type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></SCRIPT>
	<SCRIPT type="text/javascript">alert(jQuery());</SCRIPT>
</BODY></HTML>
HEREDOC;
	$a = HTML("$html");
When I execute this in XY I get a dialog that says:
A Runtime Error has occurred. Do you wish to Debug?
Line: 2
Error: Object expected
If I save the HTML to a file and open it in IE or through XY's preview or using HTML("path to html file"), it works fine and displays an alert saying "[object Object]".


My question is does the above script work for anyone else?
Or does anyone know why it isn't working for me here?

admin
Site Admin
Posts: 66347
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting: HTML with jQuery.

Post by admin »

Confirmed.

Try to save your HTML in a file and pass the filename to html(). Should work better.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Scripting: HTML with jQuery.

Post by TheQwerty »

Yeah, I was really trying to avoid having to distribute / write the HTML file.
Doesn't happen to be a simple flag or something that could fix it? :cry:

It's not so bad if I have to have the script write out the HTML file and then load that, but it sure would be nice if that wasn't needed.

admin
Site Admin
Posts: 66347
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting: HTML with jQuery.

Post by admin »

TheQwerty wrote:Yeah, I was really trying to avoid having to distribute / write the HTML file.
Doesn't happen to be a simple flag or something that could fix it? :cry:

It's not so bad if I have to have the script write out the HTML file and then load that, but it sure would be nice if that wasn't needed.
I don't know what causes this error, must be something in the browser control black box.

I use wb.Document.Write for the HTML strings
and wb.Navigate for the URLs

If you find something...

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Scripting: HTML with jQuery.

Post by TheQwerty »

Well most results seem to point at it being a security/permission issue but a lot of that is related to IE7+ and this is IE6 (thanks IT! :roll:).

If I right click the page and select refresh, then it works properly, so I believe that when you use document.write it never downloads and inserts the external script. I'm not convinced that's actually related to security/permissions though.

Maybe this means you could fix it by writing the content to the document and then loading/refreshing it, but probably not.


I've also found an ugly nasty work-around hack, thanks to a poster on Stack Overflow (Not that their solution is those adjectives, but rather having to use it here is all those things. :P)

So this works:

Code: Select all

"HTML-2"
	$html = <<<HEREDOC
<HTML>
	<HEAD>
		<SCRIPT type="text/javascript">
			function loadScript(url, completeCallback) {
				var done = false;
				var head = document.getElementsByTagName("head")[0];

				var script = document.createElement('script');
				script.src = url;
				script.onload = script.onreadystatechange = function() {
					if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
						done = true;
						script.onload = script.onreadystatechange = null;
						completeCallback();
					}
				};
				head.appendChild(script);
			}
		</SCRIPT>
	</HEAD>
	<BODY>
		<SCRIPT type="text/javascript">
			loadScript('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', function() {
				document.write(typeof jQuery);
			});
		</SCRIPT>
	</BODY>
</HTML>
HEREDOC;
	$a = HTML("$html");
The first solution from that thread might work, but I wasn't haven't success. Presumably, it was executing the rest of my script before it had completely downloaded and inserted jQuery.

It's not pretty but it hopefully means I can accomplish what I wanted without having to resort to writefile.

admin
Site Admin
Posts: 66347
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting: HTML with jQuery.

Post by admin »

FYI, the error comes right from wb.Document.Write, so anything I could comes too late.

Post Reply