Pass javascript variable to a javascript on a different website -
is possible pass javascript-variable script on site? this:
this code in page on www.myfirstsite.net:
<script> var id = 'randomstring'; </script> <script src="http://www.mysecondesite.net/processingscript.js"></script>
how can read var id in script on mysecondsite.net?
update: question wrong, explained in helpful answers @vihan1086 , others.
why never that
you should never declare variables that, has been described here
then what?
on 1 page, do:
window.globals = {}; window.globals.my_variable = 'abc';
on script, add:
var globals = window.globals; globals.my_variable;//gets 'abc'
keep variables safe in
global
place. can global variables @ once, increasing speed. don't forget wrap code in like:
(function() { //code here })();
functions
to make easier made functions:
setsharedvar (name, value) { if (!"globals" in window) { window.globals = {}; } window.globals[name] = value; } getsharedvar (name) { if (!"globals" in window) { window.globals = {}; return null; } else if (!name in window.globals) { return null; } else { return window.globals[name]; } }
examples
script 1:
setsharedvar('id', 5);
script 2:
if (getsharedvar('id') === 5) { alert('success!'); }
alerts 'success!'
Comments
Post a Comment