简体   繁体   English

包括JavaScript

[英]Including JavaScript

I know it is possible to put JavaScript is a specific .js file and then include this in any page by just doing... 我知道有可能把JavaScript放在一个特定的.js文件中,然后只需要将它包含在任何页面中......

<script src="MyJavaScript.js"></script>

I note that with respect to these .js files that are included: 我注意到这些包含的.js文件:

  1. They do not actually have to end with .js 他们实际上不必以.js结尾
  2. Their content should not be enclosed with tags 他们的内容不应附在标签上

However, I need to import some JavaScript that is in a file ending with extension .page (I am using force.com platform) where th JavaScript is enclosed with the tags. 但是,我需要导入一些文件,该文件位于以扩展名.page结尾的文件中(我使用的是force.com平台),其中JavaScript附带了标记。 The reason I need to do this is so I can write som unit tests using quint. 我需要这样做的原因是我可以使用quint编写som单元测试。 Now, my hands are tied here. 现在,我的双手被绑在这里。 The cleanest way is to obviously refactor out the JavaScript from .page file into a .js file but I can't do this. 最干净的方法是显然将.page文件中的JavaScript重构为.js文件,但我无法做到这一点。 I want to know is there any way I can just import the script snippets enclosed with tags. 我想知道有没有办法可以导入随标签附带的脚本片段。

Many thanks 非常感谢

Is this what you are looking for? 这是你想要的?

<script type="text/javascript" src="MyJavaScript.page">

John Resig has a good approach to deprecate Tags. John Resig有一个很好的方法来弃用标签。 Might be worth a look. 可能值得一看。

There is also a SO thread on this Tag issue. 此Tag问题上还有一个SO线程。 Please see here. 请看这里。

As @totero said, you can specify the script type with the attribute 正如@totero所说,您可以使用该属性指定脚本类型

<script type="text/javascript" src="myFile.annoyingExtension">

But please, don't forget to close the tag otherwhise you will get strage bugs: 但是请不要忘记关闭标签,否则你会得到惊悚错误:

<script type="text/javascript" src="myFile.annoyingExtension"></script>

Close the tag itself wont work so please, don't do this 关闭标签本身不会工作所以请不要这样做

<script type="text/javascript" src="myFile.annoyingExtension" />

You mention including Javascript, and by the looks of it, you're including them in a HTML document. 你提到包括Javascript,从它的外观来看,你将它们包含在HTML文档中。 Do you have any other programming or scripting language available to you that allows you to edit the document? 您是否有任何其他编程或脚本语言可供您编辑文档? If you can use something like PHP or ASP within the platform, you could have it read the .page file and echo it into your HTML output document (possibly stripping the <script> tags from it at your convenience). 如果您可以在平台中使用PHP或ASP之类的东西,您可以让它读取.page文件并将其回显到您的HTML输出文档中(可能在您方便时从其中剥离<script>标记)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM