遇到时,html体中的Javascript是否执行? [英] Does Javascript in the html body execute when encountered?

查看:42
本文介绍了遇到时,html体中的Javascript是否执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了ASP.net代码库,我的ASP.net技能非常有限。我正在努力理解为什么它有效,以及为什么它只适用于IE。

I have inherited an ASP.net codebase and I have very limited ASP.net skills. I am struggling to understand why something works and also why it only works in IE.

以下代码出现在页面中: -

The following code appears in a page :-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="map.aspx.cs" Inherits="Romtrac.auth_map" Culture="auto" UICulture="auto" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>
        <% = Resources.Resource.map %>
    </title>
</head>
<body>    
    <form id="adminlw_map_form" action="<%=geturl()%>" method="post"  >

    <% = Resources.Resource.loading %>...
    <textarea name="xml" id="xml" runat="server" style="display:none" cols="1" rows="1"></textarea>
    </form>
    <script language="javascript" type="text/javascript" >
      //submit main form immediately
        document.getElementById('adminlw_map_form').submit();
    </script>
</body>
</html>

此代码在ASP.net中正常运行。表单会自动提交,并且返回的页面会在Iframe中正确呈现。我的问题是;

This code runs fine in ASP.net. The form is automatically submitted and the page returned is rendered correctly within an Iframe. My question is;

1)当遇到身体中的javascript时,它是否会被执行?这是一个好的做法还是应该在事件发生时执行?

1) Does the javascript within the body just get executed when it is encountered? Is this good practice or should it be executed in response to an event?

2)为什么这在其他浏览器中不起作用?

2) Why does this not work in other browsers?

推荐答案



  1. 在浏览器完全呈现页面之前正在执行javascript。在这种情况下,表单尚未呈现,无法通过DOM访问。

执行需要在DOM之后发生由浏览器完全加载,可以通过在函数中封装调用并通过正文的onload事件调用该函数或使用jquery等javascript库挂钩到页面的load事件来实现。

The execution needs to happen after the DOM is fully loaded by the browser and can be implemented by encapsulating the call within a function and calling that function via the the onload event of the body or by using a javascript library like jquery to hook into the load event of the page.

这篇关于遇到时,html体中的Javascript是否执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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