如何将变量传递给外部js文件 [英] How to pass variable to external js file

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

问题描述

我想发送一些网址到外部js文件





代码:

  //   viewer.js(file) 
var DEFAULT_URL = ' sampletext.pdf'; // 这里我要设置pdf来自aspx.cs页面的路径
var DEFAULT_SCALE_DELTA = 1 。< span class =code-digit> 1 ;
var MIN_SCALE = 0 25 ;
var MAX_SCALE = 10 0 ;
var VIEW_HISTORY_MEMORY = 20 ;
var SCALE_SELECT_CONTAINER_PADDING = 8 ;
var SCALE_SELECT_PADDING = 22 ;
var PAGE_NUMBER_LOADING_INDICATOR = ' visiblePageIsLoading';
var DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000 ;





ple帮我解决它





谢谢.....
提前

解决方案

您的代码生成的任何javascript变量都可以从外部js文件中使用,您只需要确保在之前不读取变量它是定义的。您可以通过确保在定义变量后包含js文件,或使用类似文档就绪事件来运行访问变量的代码来实现此目的。在这个例子中,我展示了两种创建变量的方法,一种是使用文字,另一种是使用脚本管理器。如果你正在使用剃刀,那么第一种方法将适用于普通的剃刀标记。



  <   body  >  
< 表格 id = form1 runat = server >
< div >
< script type = text / javascript >
var a = '< asp:Literal ID = LiteralA runat = 服务器 / > ';
< / script >
< / div >
< / form >
< script src = External.js > < / script >
< / body >





  protected   void  Page_Load( object  sender,EventArgs e)
{
string text = Hello;

// 方法1,使用文字
LiteralA。文字=文字;

// 方法2,让ClientScript生成标记
string js = string .Format( var b ='{0}';,text);
Page.ClientScript.RegisterStartupScript( this .GetType(), setb,js, true );
}





external.js文件



< pre lang =cs> if typeof (a)!== undefined){
alert(a);
}

如果 typeof (b)!== undefined){
alert(b);
}


没有外部或内部JavaScript文件。以下是它们的工作原理:您包含一系列< script> ...< script> HTML中的元素。有些包括标签内的JavaScript(而不是'...'),这些HTML外部的一些参考文件。 JavaScript上的这些片段按顺序处理,因为它是一个更大的JavaScript文件



因此,这个问题毫无意义。但代码风格看起来非常糟糕。你可以做类似

  var  myData = {
DEFAULT_URL:' sampletext.pdf'
DEFAULT_SCALE_DELTA: 1 1
MIN_SCALE: 0 25
// ...
DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT: 5000 }

// .. 。

var myResult = someFunction(myData);
// 它甚至可以工作someFunction(数据)
// 定义如下,而不是提前,
// 这就是JavaScript的工作原理

function someFunction(数据){
var scale = data.DEFAULT_SCALE_DELTA;
scale + = // ...
if (scale> data.MIN_SCALE)
scale = data.MIN_SCALE; // 仅用于说明数据的使用
// ...
return 结果;
}





在上面显示的代码中,每个声明都可以在一个单独的文件中,但是你不应该在文件在任意位置。



如果你有多个对象与相同的属性 myData 你应该使用对象构造函数



-SA


只是提到在HTML文件中内部放置脚本参数的问题是,如果站点是HTTPS并且CSP完全启用,则指定内部脚本将触发CSP报告。 CSP文档中给出的原因是允许内部元素可以使注入更容易。



这听起来很技术性,但总有一天整个Web只支持HTTPS,更安全,性能影响可忽略不计,并提供免费安全证书(例如已经可以从Let's Encrypt和Comodo获得)。



另外还有一个完整的重新设计Web早就应该(包括能够识别和禁止未经授权的代码,这很可能是恶意的)。人们可能希望用于指定网页的许多当前语言将被更全面和更易于使用的语言所取代,从而帮助任何人能够设计自己的页面而无需学习各种不同的规范和编程语言。

i want to send some url path to external js file


Code :

// viewer.js    (file)
var DEFAULT_URL = 'sampletext.pdf' ;    // here i want to set pdf path which came from aspx.cs page 
var DEFAULT_SCALE_DELTA = 1.1;
var MIN_SCALE = 0.25;
var MAX_SCALE = 10.0;
var VIEW_HISTORY_MEMORY = 20;
var SCALE_SELECT_CONTAINER_PADDING = 8;
var SCALE_SELECT_PADDING = 22;
var PAGE_NUMBER_LOADING_INDICATOR = 'visiblePageIsLoading';
var DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000;



ple help me to solve it


thank you.....
in advance

解决方案

Any javascript variables your code generates are usable from external js files, you just have to make sure the variable isn't read before it is defined. You can do this by making sure the js file is included after your variable is defined, or using something like the document ready event to run your code that accesses the variables. In this example I'm showing two ways of creating the variable, one is using a literal, the other using the script manager. If you are using razor then the first method will work with normal razor markup.

<body>
    <form id="form1" runat="server">
    <div>
    <script type="text/javascript">
        var a = '<asp:Literal ID="LiteralA" runat="server" />';
    </script>
    </div>
    </form>
    <script src="External.js"></script>
</body>



protected void Page_Load(object sender, EventArgs e)
{
    string text = "Hello";

    // Method 1, use a literal
    LiteralA.Text = text;

    // Method 2, get ClientScript to generate the markup
    string js = string.Format("var b = '{0}';", text);
    Page.ClientScript.RegisterStartupScript(this.GetType(), "setb", js, true);
}



The external.js file

if (typeof (a) !== "undefined") {
    alert(a);
}

if (typeof (b) !== "undefined") {
    alert(b);
}


There are no "external" or "internal" JavaScript files. Here is how they work: you include a sequence of <script> … <script> elements in your HTML. Some include JavaScript inside the tag (instead of '…'), some reference files external to this HTML. And these fragments on JavaScript are processed in sequence as it was one bigger JavaScript file.

Therefore, the question makes no sense. But the code style looks really bad. You can do something like

var myData = {
   DEFAULT_URL: 'sampletext.pdf',  
   DEFAULT_SCALE_DELTA: 1.1,
   MIN_SCALE: 0.25,
   // ...
   DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT: 5000 }

// ...

var myResult = someFunction(myData);
// it will work even of function someFunction(data)
// is defined below, not in advance, 
// this is how JavaScript works

function someFunction(data) {
   var scale = data.DEFAULT_SCALE_DELTA;
   scale += // ...
   if (scale > data.MIN_SCALE)
      scale = data.MIN_SCALE; // just to illustrate the use of data 
   // ...
   return result;
}



In the code shown above, each declaration can be in a separate file, but you should not break declarations between files in an arbitrary place.

If you should have more than one object with the same properties as myData, you should use object constructor.

—SA


Just to mention that the problem with placing script arguments "internally" in HTML files is that specifying an internal script will trigger CSP reports if the site is HTTPS and if CSP is fully enabled. The reason given in the CSP doc is that allowing internal elements can make injection easier.

This may sound technical, but someday the whole Web will only support HTTPS, making it a lot more secure with negligible performance impact and with free security certificates (such as are already available from Let's Encrypt and Comodo).

As an additional aside, a complete redesign of the Web is long overdue (including being able to recognize and disallow unauthorized code, which is likely to be malicious). One might hope that the many current languages used to specify web pages would be replaced by more comprehensive and easier to use languages, helping anyone to be able to design their own pages without having to learn a variety of different specification and programming languages.


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

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