Forum Moderators: open
Above script generates two links with actions following to javascript function follow() with parameters generated from php.
Function follow gets values from php script (first link value=1 and the second value =2)
Then I want to change javascript value(clicking on any link) of global var variable with using to call that function.
But it doesn't work. Global variable always have the same declared value = strange. It prints only good passed values inside function with using innerHTML. Is it possible to pass var value from link that run js function and then change global declared value?
Look that var variable isn't change when you click any link.
Please help me with that and thx in advance..
Lucas
As a piece of development advice, download Firefox. It has a nifty Javascript error program you can use by typing "javascript:" (no quotes) in the address bar. I used it to quickly identify the sources of your trouble. Once the errors indicated by the FF JS browser were fixed, the code worked in every browser I have.
Anyway ...
The first problem was your use of the reserved word "var" as a new variable name. I changed it to "v" instead.
Once that was fixed, FF alerted me (well ... I knew it, but it found it) to the fact that you were using non-standard code with the "document.all" reference. I changed it the "document.getElementById()".
Lastly, I corrected the spelling of "chenged" to "changed", fixed the ending </script> tag in get_var_js(), and made use of that PHP function properly.
The following code is your code with my changes. When loaded into a browser, you see the initial value of "variable" (also a dangerous name ... why not change it to something less global, like "linkvar" or something?) printed by get_var at the top of the page followed by your icons and links. Clicking an icon or link shows either "1" or "2" in the "something" DIV.
I should note that it looks like you want get_var to reflect the changing value of the variable, however you are only invoking it once, from the server, so it can only show that variable's default value ("strange"). The value that is displayed in the "something" DIV is the changing variable value.
Have fun! (and get creative with your variable names to avoid conflicting with built-in or reserved variable names. :)
<html> <head> <script type="text/javascript"> <!-- var variable = 'strange'; function follow(v) { variable = v; document.getElementById('something').innerHTML = variable; } function get_var(value) { changed = eval(value); document.write(changed); } --> </script> <?php function get_var_js($var_js) { $x = "<script> get_var('" . $var_js . "'); </script>"; return print $x; } function ShowLink($nr_link, $nr_style, $link_name) { print "<div><a href=\"\"><img class=\"ikona$nr_link\" src=\"./_grafika/menu$nr_link.png\" alt=\"ikona: Link\" /></a><p class=\"link$nr_link\"><a class=\"lnk$nr_style\" href=\"javascript: void(0);\" onclick=\"javascript: follow($nr_link);\">$link_name</a></p></div>"; } ?> </head> <body> <center><?php get_var_js("variable"); ShowLink(1, 1, "START"); ShowLink(2, 2, "Something"); ?></center><br> <div id="something"></div> </body> </html>
print "<div><a href=\"javascript:void follow($nr_link);\"><img class=\"ikona$nr_link\" src=\"./_grafika/menu$nr_link.png\" alt=\"ikona: Link\" /></a><p class=\"link$nr_link\"><a class=\"lnk$nr_style\" href=\"javascript:void follow($nr_link);\">$link_name</a></p></div>"; ;)
You're right I wanted to do something impossible. Thanks anyway for answers. Now I've done it using forms and posting variables.