This question already has answers here:
JQuery to load Javascript file dynamically
(2 answers)
Dynamically load a JavaScript file
(30 answers)
Closed 8 years ago.
Inside one of my views, I'd like to load this script:
<script type="text/javascript" src="https://domain.com" id="hello" name="abc"></script>
Is it possible to do that?
Manually appending it:
$('head').append('<script type="text/javascript" src="https://domain.com" id="hello" name="abc"></script>')
Using $.getScript:
$.getScript('https://domain.com').done(function () {
// loaded!
});
RequireJS:
require.config({
paths: {
"myscript": "https://domain.com"
}
});
require(['myscript'], function () {
// loaded!
});
Related
This question already has answers here:
Dynamically load JS inside JS [duplicate]
(12 answers)
Closed 4 years ago.
I have a script tag with an src, how can I reload the src with java script code, this is the code I have:
<script src='./script.php'>
<script>
function reload() {
//the code to reload the src
}
</script>
Snippet:
<script>
function reload() {
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.src= './script.php';
head.appendChild(script);
}
reload();
</script>
This question already has answers here:
Conditionally load JavaScript file
(8 answers)
Closed 4 years ago.
I have specific javascript code
<script type="text/javascript" async="true" data-ad-type="iframe v2.0" charset="utf-8" src="//mydomaincom.com/generic/uni.php?g=5"></script>
that work on all pages, but on /ucp.php?mode=register I dont want to execute the code. How to do this?
I made this code but not working (two script inside?)
<script>
if ( window.location.pathname == '/ucp.php?mode=register'{
} else {
<script type="text/javascript" async="true" data-ad-type="iframe v2.0" charset="utf-8" src="//sk.search.etargetnet.com/generic/uni.php?g=5"></script>
}
</script>
The answer should be with PHP too, if with PHP i can make it.
Try this.
if ( window.location.pathname != '/ucp.php'){
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src","//sk.search.etargetnet.com/generic/uni.php?g=5")
fileref.setAttribute("async","true")
fileref.setAttribute("data-ad-type","iframe v2.0")
fileref.setAttribute("charset","utf-8")
}
This question already has answers here:
JavaScript Code Inside <script> Tag
(3 answers)
Closed 8 years ago.
I'm messing around with objects and methods, and I have this really simple example that I use to test with:
var shout = {
hello: function(variable){
console.log("Hello " + variable);
}
};
shout.hello("World");
And this works fine. However, if I place the object shout in an external file, and then run shout.hello("world"); I get nothing:
//external file: test.js
var shout = {
hello: function(variable){
console.log("Hello " + variable);
}
};
<!-- my html document -->
<script src="test.js">
shout.hello("World");
</script>
What am I doing wrong?
From MDN:
script elements with an src attribute specified should not have a script embedded within its tags.
You need two separate script tags, one to import your external script, and another to call the function, for example:
<script src="test.js"></script>
<script>
shout.hello("World");
</script>
You need two separate script tags, the content of a tag with src attribute is ignored.
<script src="test.js"></script>
<script>
shout.hello("World");
</script>
This question already has answers here:
Loading cross-domain endpoint with AJAX
(9 answers)
Closed 8 years ago.
How can I load a page using jQuery, AJAX, and JSON? I've tried something like this, but it is not working.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
<div id="wsww">content.<div>
<script type="text/javascript">
$.get('https://www.google.com/')
.success(function(data) {
$('#wsww').html(data);
});
</script>
You don't need the .success function, instead use a callback function... try this
$.get("<url>", function(data){
$('#www').html(data);
});
Cross Domain Access
HTML
<div id="Content">
</div>
Jquery
$.getJSON('http://whateverorigin.org/get?url=' +
encodeURIComponent('http://google.com') + '&callback=?',
function(data){
$("#Content").html(data.contents);
});
Reference
Whateverorigin.org/
This question already has answers here:
Can we call the function written in one JavaScript in another JS file?
(10 answers)
Closed 8 years ago.
I have a function in my separate js file called hello().
On my page I have this js:
<script type='text/javascript'>//do hello fnc</script>
How can I get the on page script to call the function in my main js file?
Call it in the src attribute of another script tag, before referring to any of its functions in this script.
<script type='text/javascript' src='main.js'></script>
<script type='text/javascript'>
hello();
// Anything defined in previously included js files like main.js
// is now available, as well as anything defined on this page.
</script>
Load the external file first and call the function:
<script src=external.js></script>
<script>
hello();
</script>
You just need to include the external js file:
<script type="text/javascript" src="MyOtherFile.js"></script>
And then wherever in you code you want to call the funciton:
<script type="text/javascript">hello();</script>