如何在javascript中传递变量

我正在建立一个基于这个网站的书籍小册子: http : //www.latentmotion.com/how-to-create-a-jquery-bookmarklet/

这是bookmarlet的代码:

javascript:(function(){ var head=document.getElementsByTagName('head')0], script=document.createElement('script'); script.type='text/javascript'; script.src='http://myserver.com/bookmarlet-remote.js?' + Math.floor(Math.random()*99999); head.appendChild(script); })(); void 0 

我如何将bookmarlet(上面的代码)中的变量传递给bookmarlet-remote.js?

我在var myNewvar =’myValue’之后尝试过,没有成功,任何想法?

页面上的所有JS代码(包括包含的书签代码和脚本)都可以访问全局范围。 如果您定义一个没有var前缀的变量,它将可用于所有其他脚本。

明确这一点可能是一个好主意。 做window.myVar = "foo"; 清楚地表明您正在使用全局变量。

在函数中使用var使其成为该函数的本地。 要使其全局化,您必须将其添加到window范围内,因此:

 window.newVariable = window.newVariable || 'Your new value here'; 

要么

 window['newVariable'] = 'Your new value here'; 

你要创建一个公共变量。

 window.rnd = Math.floor(Math.random()*99999); 

在bookmarlet-remote.js中,您只需访问该变量。