如何在HTML中显示来自外部JavaScript的变量。 Internet Explorer 7 [英] How to display variables from external JavaScript in HTML. Internet Explorer 7

查看:98
本文介绍了如何在HTML中显示来自外部JavaScript的变量。 Internet Explorer 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这是一个可怕的新手问题,但我一直试图解决它几天尝试不同的方法,所以我只想问你会做什么。



我正在尝试创建一个在工作中使用的Web程序,并且我有这样的设置:

Windows 7
IE 7 - 无法升级。



网站不是虚拟主机,基本上我的桌面上有一个带有html / css / js文件的文件夹,我使用IE来运行脚本,没有主机。



我想在外部JS文件中保留一组变量,主要是字符串,并将JS拉入不同的HTML页面。我希望它能够在文档的加载上写入..没有准备好。它不一定是用户动态的。



另外,当我制作js文件时,它是否必须有一个标题..就像HTML有doctypes?



我非常感谢您的帮助,因为我正在努力学习,并将继续从我这里继续。我的设置与大多数不同,我不知道哪个部分导致了我的问题,所以我最终分手并发布了。

解决方案

当您编写JavaScript文件时,它不必具有任何标头或文档类型。例如,你可以有一个像这样的variables.js文件:

  var x =abc; 
var y =def;

并且有许多包含variables.js的HTML文件,如下所示:

 <!doctype html> 
< html lang = en>
< head>
< meta charset = utf-8>
< title> title< / title>
< / head>
< body>
<! - 页面内容 - >
< script src =variables.js>< / script>
< script>
alert(x);
< / script>
< / body>
< / html>

并且您的变量应该在那里可用。任何包含在对variables.js的引用之后的脚本应该可以访问以前包含的所有内容,而无需监听任何事件。



如果需要听取事件,然后我建议使用jQuery或其他JavaScript框架。 jQuery的一个例子是:

$ $ p $ $ $ $ $ $ $ $ ;
});

更改DOM元素的更高级示例:

 <!doctype html> 
< html lang = en>
< head>
< meta charset = utf-8>
< title> title< / title>
< / head>
< body>

< p>选择变量:< / p>
< p>
< a href =#id =show-x>显示x< / a>
< a href =#id =show-y>显示y< / a>
< / p>
< p>值:< / p>
< p id =value>< / p>

< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js>< / script>
< script src =variables.js>< / script>
< script>
$('#show-x')。click(function(e){
e.preventDefault();
$('#value')。html(x);
});
$('#show-y')。click(function(e){
e.preventDefault();
$('#value')。html(y);
});
< / script>

< / body>
< / html>


I realize this is a horribly newbie question, but Ive been trying to fix it for days trying different methods so I just wanted to ask what would you do.

I am attempting to create a web program to use at work, and I have this setup:

Windows 7 IE 7 - Cannot Upgrade.

The "website" is not a webhost, basicly I have a folder on my desktop with html/css/js files and I use IE to run the scripts, no host.

I want to keep a set of vars, mostly strings, in an external JS file and pull the JS into different HTML pages. I want it to write on load of the document.. not on ready. It does not have to be user dynamtic.

Also, When I make the js file, does it have to have a header.. like HTML has doctypes?

I really appreciate your help as I am trying to learn and will cont on my own from here. My setup is much different than most, and im not sure which part was causing my problem so I finally broke down and posted.

解决方案

When you write your JavaScript file it doesn't have to have any header or doctype. For example you can have a variables.js file that looks just like this:

var x = "abc";
var y = "def";

and have many HTML files that include variables.js like this:

<!doctype html>
<html lang=en>
  <head>
    <meta charset=utf-8>
    <title>title</title>
  </head>
  <body>
    <!-- page content -->
    <script src="variables.js"></script>
    <script>
      alert(x);
    </script>
  </body>
</html>

and your variables should be available there. Any script that is included after the reference to your variables.js should have access to everything that was included before without the need to listen to any events.

If you need to listen to the events then I suggest to use jQuery or some other JavaScript framework. An example for jQuery would be:

$(window).load(function() {
  alert(x);
});

A more advanced example of changing the DOM elements:

<!doctype html>
<html lang=en>
  <head>
    <meta charset=utf-8>
    <title>title</title>
  </head>
  <body>

    <p>Select variable:</p>
    <p>
      <a href="#" id="show-x">Show x</a>
      <a href="#" id="show-y">Show y</a>
    </p>
    <p>Value:</p>
    <p id="value"></p>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="variables.js"></script>
    <script>
      $('#show-x').click(function (e) {
        e.preventDefault();
        $('#value').html(x);
      });
      $('#show-y').click(function (e) {
        e.preventDefault();
        $('#value').html(y);
      });
    </script>

  </body>
</html>

这篇关于如何在HTML中显示来自外部JavaScript的变量。 Internet Explorer 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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