0

I am trying to include external javascript to a document. Let's say that external js has the following code.

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. The function is being called from php file included on the page somehow. 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.

Q: how do I ensure that I include my javascript BEFORE the php file given my situation?

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. 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

EDIT:

I think the real problem is that I am dealing with an iframe that's included inside a main document. In this case, is there a way to include my external javascript file inside the main document from console? Including scripts from console only affects the iframe instead of the main document.

1
  • 1
    write the php code you use to call that function Commented Mar 17, 2014 at 22:51

2 Answers 2

3

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. 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:

$.getScript("http://myjs", function() {
    // The script has been loaded, you can call myFunction now
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer T.J. I tried this already unfortunately. The problem is that the predefined php file needs to call the function in myjs. After I include myjs via getScript, it seems that the php file still cannot call the function. Suppose myFunction() is defined in the newly included javascript file my.js. I can call myFunction from console, but the php file that's loaded from server still cannot call myFunction(). I am not familiar with php and not sure how to solve this.
I assume the entire website consists of multiple iframes, and the php that's making the function call is in the outside iframe while I am including myjs script only in the inside iframe.
@MaximusS: The PHP is running on the server. The JavaScript is running on the client. The PHP code cannot call the JavaScript. All it can do is output JavaScript code that calls the JavaScript. It sounds like it's doing that, but the code it outputs is running before the script file is loaded.
0

PHP is always included before JavaScript. PHP is executed by the server before it's sent to the client, and JavaScript is executed by the client.

Why can't you just put a regular <script type="text/javascript" src="http:/myjs"></script> in your head?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.