如何在JavaScript中传递变量 [英] How to pass a variable in javascript

查看:189
本文介绍了如何在JavaScript中传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于以下站点构建一本小册子: http://www.latentmotion.com/how-to-create-a-jquery-bookmarklet/

I am building a bookmarlet based on this site: http://www.latentmotion.com/how-to-create-a-jquery-bookmarklet/

这是小册子的代码:

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?

How I can pass a variable from the bookmarlet (above code), to bookmarlet-remote.js ?

我已经尝试过var myNewvar ='myValue',但没有成功,知道吗?

I've tried after var myNewvar='myValue', without success, Any Idea?

推荐答案

页面上的所有JS代码(包括书签代码和包含的脚本)都可以访问全局范围.如果定义不带var前缀的变量,则所有其他脚本都可以使用该变量.

All JS code on a page (including bookmarklet code and scripts included have) have access to the global scope. If you define a variable without the var prefix it will be available to all other scripts.

对此进行明确说明可能是一个好主意. window.myVar = "foo";可以清楚地表明您正在使用全局变量.

It might be a good idea to be explicit about this. do window.myVar = "foo"; to clearly signal that you are working with global variables.

这篇关于如何在JavaScript中传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆