JavaScript在页面上有效,如果被引用则不起作用 [英] Javascript works on page and not working if referenced

查看:63
本文介绍了JavaScript在页面上有效,如果被引用则不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想从载入中的javascript中的代码中读取属性,然后我很高兴地做到这一点:

Hi,
I want to read a property from the code behind in my javascript on load, and i am doing it happily like this:

<script language="javascript">
    $(window).load(function() {
        if (''<%= IsNewRecord %>'' == ''True'') {
            alert("new record");
        }
    });
</script>



属性"IsNewRecord"在我的代码中,如下所示:



where property ''IsNewRecord'' is in my code behind like below:

protected bool IsNewRecord
   {
       get { return true; }
   }



到这里为止都可以正常工作,但是当我将javascript代码移动到外部文件时,例如说test.js并在页面上的脚本标签中引用它,该行为就会停止并且没有错误.



It works fine till here, but when i move my javascript code to a external file say test.js and reference it in my script tag on the page, the behaviour stop working with no errors.

<script src="scripts/test.js" type="text/javascript"></script>



为什么?



Why ?

推荐答案

(window).load(function(){ if(''<%= IsNewRecord%>''==``True''){ alert(新记录"); } }); </script>
(window).load(function() { if (''<%= IsNewRecord %>'' == ''True'') { alert("new record"); } }); </script>



属性"IsNewRecord"在我的代码中,如下所示:



where property ''IsNewRecord'' is in my code behind like below:

protected bool IsNewRecord
   {
       get { return true; }
   }



到这里为止都可以正常工作,但是当我将javascript代码移动到外部文件时,例如说test.js并在页面上的脚本标签中引用它,该行为就会停止并且没有错误.



It works fine till here, but when i move my javascript code to a external file say test.js and reference it in my script tag on the page, the behaviour stop working with no errors.

<script src="scripts/test.js" type="text/javascript"></script>



为什么?



Why ?


当网络浏览器需要网页时,它会向服务器发出html文件(在本例中为aspx文件)的HTTP GET.通过标识.aspx文件扩展名,Web服务器(在本例中为IIS)知道客户端请求的文件是ASP.NET Web表单.它使用它作为提示,它需要在您的Web表单中执行动态内容,并将输出发送到TCP流.

当客户端浏览器接收到您的HTML时,它将对其进行解析,并列出HTML中所有外部资源的列表,例如CSS,图像和您的javascript.然后,为每个这些资源发出单独的HTTP GET.一旦收到它们,它们就会组装并将页面呈现给用户.

在上一步中,浏览器请求了您的.js文件.服务器收到了请求,并愉快地将javascript传递到了您的浏览器.由于文件扩展名是.js,而不是.aspx,因此它不知道您是否希望它解析javascript并查找您的代码隐藏引用.它刚交付了货物.

您有三个选择.

1.像原来一样,将javascript嵌入html文件中.

2.将独立的javascript文件重命名为.aspx扩展名,然后在.aspx中添加 ContentType @Page指令 [ ^ ]文本/javascript.在您的HTML中,链接到.aspx文件.
When a web browser wants a web page it issues an HTTP GET to the server for the html file, or in this case, your aspx file. The web server, in this case IIS, knows that the file that the client requested is an ASP.NET web form by identifying the .aspx file extension. It uses that as a cue that it needs to execute the dynamic content in your web form, and send the output to the TCP stream.

When the client browser receives your HTML, it parses it, and makes a list of all external resources in the HTML such as CSS, images, and your javascript. It then issues a separate HTTP GET for each of those resources. Once it has received them all it assembles and renders the page to the user.

In that previous step the browser made a request for your .js file. The server received the request and happily delivered the javascript to your browser. Since the file extension is .js, not .aspx it didn''t know that you wanted it to parse the javascript and look for your codebehind reference. It just delivered the goods.

You have three options.

1. Just embed the javascript in the html file like you did originally.

2. Rename the standalone javascript file to a .aspx extension and in the .aspx add a ContentType @Page directive[^] to text/javascript. In your HTML, link to the .aspx file.
<script src="scripts/test.aspx" type="text/javascript"></script>


IIS现在将解析并编译您的javascript文档,从而正确地呈现您的代码隐藏;由于已正确设置MIME内容类型,因此即使扩展名为.aspx,浏览器也会将其视为javascript.

3.在IIS配置(或web.config)中,可以配置IIS,以便将所有.js文件视为Web表单.

多么不错,三个选项!


IIS will now parse and compile your javascript document, correctly rendering your codebehind; since the MIME content type is correctly set, the browser will treat it as javascript, even though it has a .aspx extension.

3. In the IIS configuration (or web.config) you can configure IIS so that it treats all .js files as web forms.

How nice, THREE options!


这不是很明显吗?查看部分''<%= IsNewRecord %>''.这部分代码不是Javascript.它是由您后面的代码生成的,因此您的页面是使用
生成的
Isn''t it obvious? Look at the part ''<%= IsNewRecord %>''. This part of code is not Javascript. It is generated by your code behind, so your page is generated with either

if ('True' == 'True') {
    alert("new record");
}







or

if ('False' == 'True') {
    alert("new record");
}



因此,将根据IsNewRecord返回并动态生成有效的Javascript代码来动态生成Javascript代码,这些Java代码在每个HTTP请求中各不相同.

当您将此文件放在* .ASPX文件之外时,该代码将按原样发送到同一页面的请求中,这不是有效的Javascript代码. Javascript解释器失败,您的服务器隐藏了错误处理(这是生产中的常规选项),因此您根本无法进行处理.

—SA



So, Javascript code is generated dynamically depending on that IsNewRecord returns and results in valid Javascript code, one or another, different on each HTTP request.

When you put this file outside of you *.ASPX file, the code is delivered on the request for the same page as is, which is not a valid Javascript code. Javascript interpreter fails, your server hides error processing (which is the normal option for production), so you simply don''t get your processing.

—SA


这篇关于JavaScript在页面上有效,如果被引用则不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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