简体   繁体   English

包括外部javascript文件

[英]including external javascript file

I am trying to include external javascript to a document. 我正在尝试将外部JavaScript包含到文档中。 Let's say that external js has the following code. 假设外部js具有以下代码。

function myFunction() {
  console.log("hello");
}

and I include it from console by 我通过控制台将其包括在内

var script = document.createElement('script');
script.src = "http://myjs";
document.getElementsByTagName('head')[0].appendChild(script);

but then I still get myFunction() is undefined error. 但是我仍然得到myFunction()是未定义的错误。 The function is being called from php file included on the page somehow. 该函数是从页面上包含的php文件中以某种方式调用的。 Interestingly, appending my external javascript right after the head tag in the document was not enough for it to precede the function call from the loaded php file. 有趣的是,在文档中的head标记之后附加我的外部javascript不足以使其在从已加载的php文件进行函数调用之前。

Q: how do I ensure that I include my javascript BEFORE the php file given my situation? 问:根据情况,如何确保在php文件之前包含我的JavaScript?

EDIT: this is the hierarchy of all the sources 编辑:这是所有源的层次结构

mysubsite.com
  myfolder
    mypage.html

mysite.com
  myfolder
    main.php
    problem.php

problem.php is calling function myFunction() but it's not included anywhere yet. problem.php正在调用函数myFunction()但尚未在任何地方包含它。 So I try to define the function in an outside js file and include it in mypage.html, but problem.php still comes before the included javascript in mypage.html 因此,我尝试在外部js文件中定义该函数并将其包含在mypage.html中,但问题.php仍然位于mypage.html中包含的javascript之前

EDIT: 编辑:

I think the real problem is that I am dealing with an iframe that's included inside a main document. 我认为真正的问题是我正在处理包含在主文档中的iframe。 In this case, is there a way to include my external javascript file inside the main document from console? 在这种情况下,是否可以通过控制台将我的外部javascript文件包含在主文档中? Including scripts from console only affects the iframe instead of the main document. 从控制台包含脚本只会影响iframe,而不会影响主文档。

What you're doing is fine, but then you have to wait for the file to load. 您正在执行的操作很好,但是随后必须等待文件加载。 Most browsers will raise the load event on the script element although some older versions of IE use onreadystatechanged instead. 尽管某些旧版本的IE使用onreadystatechanged代替,但大多数浏览器都会在脚本元素上引发load事件。 But since you're using jQuery (from the tags on the question), you don't have to worry about that, you can just use $.getScript : 但是由于您使用的是jQuery(来自问题标签),因此您不必担心,只需使用$.getScript

$.getScript("http://myjs", function() {
    // The script has been loaded, you can call myFunction now
});

PHP is always included before JavaScript. PHP始终包含在JavaScript之前。 PHP is executed by the server before it's sent to the client, and JavaScript is executed by the client. PHP由服务器执行,然后再发送给客户端,而JavaScript由客户端执行。

Why can't you just put a regular <script type="text/javascript" src="http:/myjs"></script> in your head ? 为什么不把普通的<script type="text/javascript" src="http:/myjs"></script>放在head

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

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