1

I want to Run a Function from .js file without import in in my page I use this code :

var js =("../../../static/js/file.js");
js.hi();

and show this Error in console :

Uncaught TypeError: undefined is not a function

But I have a function with hi name !

Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146
mohammad
  • 275
  • 3
  • 16
  • What do you mean by "without import"? Without "importing" it one way or another, there is no way it can be run. Do you mean "without using ` –  Jan 12 '15 at 07:51
  • Also have a look at this answer of me: https://stackoverflow.com/a/72335692/9868445, if you meant without adding another script tag. – aderchox May 22 '22 at 08:01

1 Answers1

2

Method 1: Use jQuery.getScript.

Think of jQuery.getScript() as the JavaScript equivalent to CSS’s @import. Take it one step further and use it to load JavaScript only when it is needed and you have a lean combination.

Sample:

if ($('a.email').length > 0){
    $.getScript('js/jquery.mailto.js',function(){
        $('a.email').mailto();
    });
}

This small snippet of code that will check for the existence of an object, in this case that object is any anchor link with a class of email. If this object is found on a page, a corresponding .js file (js/jquery.mailto.js) will be loaded and a callback function initiated ($(‘a.email’).mailto();). If the object is not found, the script will not be loaded. This assumes that the snippet is enclosed in a document ready event.

Source: External JavaScript on Demand With $.getScript()

So you can do something like

if(condition){
  $.getScript('../../../static/js/file.js',function(){
      hi();
  });
}

Method 2: Dynamically loading external JavaScript and CSS files

function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

loadjscssfile("myscript.js", "js") //dynamically load and add this .js file
loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file
loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • I use it and get this Erro : GET http://meip/static/js/mobile_mailbox.js?_=1421047136030 404 (Not Found)jquery.min.js:6 x.ajaxTransport.x.support.cors.e.crossDomain.sendjquery.min.js:6 x.extend.ajaxjquery.min.js:6 x.each.x.(anonymous function)jquery.min.js:6 x.extend.getScript2:219 m.port1.onmessage – mohammad Jan 12 '15 at 07:21
  • Does this file exist? Are you making cross domain requests? – Chankey Pathak Jan 12 '15 at 07:25
  • Check the file URL. The error 404 says it can't access the path, so either the path is incorrect or there's permission issue. – Chankey Pathak Jan 12 '15 at 07:39
  • Going by the path `../../..`, you're trying to traverse directories to a directory which is not visible on the web site. Start by using your browser to access the JavaScript file directly and then put the URL you used into the script loader. – Steve Jan 12 '15 at 11:46