我是初学者!我需要这样的东西:
C:\MyHtmlPage.html "string_that_I_need_to_pass_to_the_javascript_code_inside_MyHtmlPage"
并将“string_that_I_need_to_pass_to_the_javascript_code_inside_MyHtmlPage”放入其中:
var externalstring
有类似的事情吗? 谢谢!
编辑:
我找到方法了! 在 DOS 命令行中我写道:
start firefox "file:///C:/MyHtmPage.html?externalstring=string_that_I_need_to_pass_to_the_javascript_code_inside_MyHtmlPage"
在 javascript 中我添加了这个(http://papermashup.com/read-url-get-variables-withjavascript/):
var externalstring = getUrlVars()["externalstring"];
alert(externalstring);
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
function(m,key,value) {
vars[key] = value;
});
return vars;
}
谢谢大家的建议! =)
最佳答案
您可以使用如下查询参数:
C:\MyHtmlPage.html?parm=string_that_I_need_to_pass_to_the_javascript_code_inside_MyHtmlPage
然后,在您的页面 JavaScript 中,您可以使用 window.location.search
检索 parm=xxxx 片段并根据需要解析出 xxx。如果需要,此格式允许您传递多个不同的参数。这是将多个参数嵌入到 URL 中的通用方法。然后可以通过页面中的 javascript 或服务器(如果 URL 来自 Web 服务器)来解析它们。
关于javascript - 有没有办法使用命令行将字符串传递给 html javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12654957/