2

I know that we can call a JavaScript function on page load using jQuery:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript">  
    <script type="text/javascript">  
        $(document).ready(function() {  
            alert("Hello World!");  
        });  
    </script>

But how to do it if the function is inside an external ".js" file?

And another question please, can "src" tag have a value like this: src = "http://google.com"? I mean without the ".js"suffix at the end of it.

Thanks

Edit: What I mean by "external" is just another file on my desktop, not on the web.

3
  • Can you elaborate on what do you mean by calling a function in an external js file? Commented Oct 17, 2016 at 11:05
  • I'm no expert but i believe that all Javascript files (their contents, to be precise ) loaded trough script tags belong to the same scope, hence you can just invoke the function normally ( provided, of course, that the file had already loaded ). As for your second question, I see no reason why you shouldn't be able to do such thing, provided that the server returns valid Javascript Commented Oct 17, 2016 at 11:05
  • Javascript is a very dirty beast, any script can add however many globals it likes, and override the globals created by other scripts. You are expected to export no more than one global variable and preferably none and use a module loading system, but there is nothing stopping you going crazy and manipulating whatever you want except hopefully yourself! Commented Oct 17, 2016 at 11:21

6 Answers 6

1

In external .js-file you can do the same. But file should be after jQuery in DOM.

Without jQuery you can use next code:

window.onload = function () {alert('Hello World!')

And another question please, can "src" tag have a value like this: src = "http://google.com"? I mean without the ".js"suffix at the end of it.

Yes, you can.

Sign up to request clarification or add additional context in comments.

3 Comments

I think I saw the "onload" before, but what I want to do is working on a page content without opening it in the window, so I think that "onload" isn't what I look for.
I am making a Chrome extension so the content script must be JavaScript files, and I don't know about jQuery. P.S. I am novice in both HTML and JS.
Is there anyway that I can invoke the function from within the external ".js" file and without opening the URL in the window? That's exactly what I am looking for.
1

All the JavaScript files you load are on same scope: your DOM. So it doesn't matter from which file you are loading one function and from which file you are loading another function. As long as you load all required files there's no problem.

For instance I recently developed an OOP JS project and I had js/namespace/class.js files. Of course on class used another one (even from different namespaces). I had an insane long head (with lot of imports) but everything just worked fine.

Regarding your second question: The src field must point to a valid JS resource. src="//something.org/resource" in valid if it's a REST js resource (routed by server) instead a plain file. It just matters that it is a valid JS "string"

Let's take this example:

//mysite.org/rest/js1

var greet = function() {
    alert("Hello World!");
}

//mysite.org/rest/js2

$(document).ready(function() {
    greet();
}

http[s]://mysite.org

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
<script src="//mysite.org/rest/js1" type="text/javascript"></script>
<script src="//mysite.org/rest/js2" type="text/javascript"></script>

Swapping import for js1 and js2 may break it all, so be carefull with order.

Comments

0

Probably you can use the same

$(document).ready(function() {  
   alert("Hello World!");  
});

In the external file?

Comments

0

Yes, it doesn't need to have a .js extension you can load any js file from any server, no matter how malicious the site may be. It does have to be a valid js file served with the appropriate header though. Google.com isn't one... it's a html file. If you are on https: domain all resources will also need to be HTTPS or they will be skipped

In your above example $ is an external function loaded from one of googles pages!

Comments

0
<script type="text/javascript">
function myfunction() {
   alert('it has loaded');
};
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript" onload="myfunction()">  

Just make sure to define your function BEFORE the code that calls the external code. Just in case the code is cached and executed immedeately before the page arrives at your code.

You can do a complete javascript version too:

function myfunction() {
    alert('script has loaded');
}
function loadJS(url, callback){
    //url is URL of external file, callback is the code
    //to be called after file has loaded

    var scriptTag = document.createElement('script');
    scriptTag.src = url;

    scriptTag.onload = callback;
    scriptTag.onreadystatechange = callback;

    document.body.appendChild(scriptTag);
};

loadJS('http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js', myfunction);

1 Comment

Regarding the complete JS version, I built the code but it didn't make an alert. P.S. What I mean by "external" file is another file on my desktop, not on the web.
0

You can load all javascript files inside script tag, then you can call any function like :

$(document).ready(function() {  
       yourFuntion();  
    }); 

PS: do not forget to call your function before javascript files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.