English 中文(简体)
a.js文档中添加变量
原标题:Add variables to a .js file

I m 寻找在外部档案中张贴变量的方法。

类似:

<script type="text/javascript" src="/js/script-starter.js?var=value"></script>

然后我要说几句(因此,价值)。 这样做是否可行?

谢谢!

最佳回答

Yes, it is possible. You will have to look for all script tags in the DOM (make sure the DOM is loaded before attempting to look in it) using the document.getElementsByTagName function, loop through the resulting list, find the corresponding script and parse the src property in order to extract the value you are looking for.

大致如下:

var scripts = ​document.getElementsByTagName( script );
​for (var i = 0; i < scripts.length; i++)​ {
    var src = scripts[i].src;
    // Now that you know the src you could test whether this is the script
    // that you are looking for (i.e. does it contain the script-starter.js string)
    // and if it does parse it.
}​

关于<代码>src的分类,属性是指从中提取指示值的查询,您可使用

问题回答

如果该笔文字是服务器式文字,接收可变的盘点和作为产出回归的javascript代码,则有可能。

How about declaring the variable before script file loading:

<script type="text/javascript">var myVar = "Value"</script>
<script type="text/javascript" src="/js/script-starter.js"></script>

因此,你可以在你的文字中使用这一变量。 这可能是最容易的方法之一。

为什么不把变数隐藏在一或一页。

<a id="myHiddenVar" style="display:none;">variables</a>

之后,请见您。 j 随同接触

var obj = document.getElementById("myHiddenVar").innerHTML;

或 j

var obj = $("#myHiddenVar").text();
function getJSPassedVars(script_name, varName) {
    var scripts = $( script ),
        res = null;

    $.each(scripts, function() {
        var src = this.src;
        if(src.indexOf(script_name) >= 0) {
            varName = varName.replace(/[[]/,"\[").replace(/[]]/,"\]");
            var string = new RegExp("[\?&]"+ varName +"=([^&#]*)"),
                args = string.exec(src);
            if(!args.length) { 
                res = null; 
            } else { 
                res = args[1]; 
            }
        }
    });
    return res;
}

console.log(getJSPassedVars( script-starter.js ,  var ));

在我看来,我的手法结构如下:

MyApp/
     index.html
     jquery.js
     my.js

以上职能载于my.js with console.log ,并将其列入index.html>。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.