This topic is locked

Adding custom javascript code

11/8/2006 6:05:12 AM
PHPRunner General questions
R
runey author

I just tried to enter some javascript code... Basically just added some code to change the image of a button when it is clicked... Normally I would just some code into the <head> like this

<script language="JavaScript">

<!-- Hide from old browsers
var NN3 = false;

image1= new Image();

image1.src = "IMAGE FILE OFF";

image1on = new Image();

image1on.src = "IMAGE FILE ON";

image2= new Image();

image2.src = "IMAGE FILE OFF";

image2on = new Image();

image2on.src = "IMAGE FILE ON";
function on3(name) {document[name].src = eval(name + "on.src");}

function off3(name) {document[name].src = eval(name + ".src");}

NN3 = true;
function on(name) {if (NN3) on3(name);}

function off(name) {if (NN3) off3(name);}

// -->

</script>


and add some code into the <body> like this

<a href="YOUR LINK" onmousedown="on('image1');" onmouseup="off('image1')"><img src="IMAGE FILE OFF" border="0" name="image1"></a><br>

<a href="YOUR LINK" onmousedown="on('image2');" onmouseup="off('image2')"><img src="IMAGE FILE OFF" border="0" name="image2"></a>


however this just generates an error message <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=3789&image=1&table=forumtopics' class='bbc_emoticon' alt=':huh:' />
Any tips on how to enter custom javascript code will be gretly appreciated <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=3789&image=2&table=forumtopics' class='bbc_emoticon' alt=':)' />

Alexey admin 11/8/2006

Hi,
there are no any special requirements on embedding custom javascript code into PHPRunner pages.
Debug your code to get it working.

R
runey author 11/8/2006

I tested the javascript code in a blank html page, ie no code other than the javascript, and it works just fine...
So if it's not the javascript causing the error then it must be it's interaction with the PHPRunner generated code... i.e. I'm putting it in the wrong place or something...
The code was added to menu.htm and this is the error that is generated...

Smarty error: [in menu.htm line 21]: syntax error: unrecognized tag: document[name].src = eval(name + "on.src"); (Smarty_Compiler.class4.php, line 436)

Alexey admin 11/8/2006

I'm sorry, my fault.
You can not use curly brackets - {} in your javascript code.

Otherwise enclose the code in {literal} .. {/literal} block.

So your code should look like this:

{literal}

<script language="JavaScript">

<!-- Hide from old browsers

var NN3 = false;

image1= new Image();

...

function off(name) {if (NN3) off3(name);}

// -->

</script>

{/literal}

R
runey author 11/8/2006

Thanks for that <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=12647&image=1&table=forumreplies' class='bbc_emoticon' alt=':D' /> I'll try it tonight...

J
Jean 11/11/2006

I'm sorry, my fault.

You can not use curly brackets - {} in your javascript code.

Otherwise enclose the code in {literal} .. {/literal} block.

[...]


Mmmh... I prefer to avoid {literal} and replace each { by {ldelim} and each } by {rdelim} so that I may push variables from php to javascript using the Smarty syntax {$variable}.
But perhaps a {/literal}{$variable}{literal} would do the trick too, I didn't try.
Jean