Javascript, async, send custom var - javascript

I'm using the following code http://friendlybit.com/js/lazy-loading-asyncronous-javascript/ to make async calls but now I want to send custom var to this script and cannot figure it out.
(function() {
function async_load(){
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'http://yourdomain.com/script.js';
var x = document.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
}
if (window.attachEvent)
window.attachEvent('onload', async_load);
else
window.addEventListener('load', async_load, false);
})();
Now I want to have custom var that is sent to "script.js". That's all, a simple var to be sent to JS. I've even tried by using query strings but no results.
Help please.

Just assign it to the global scope (with window.varname = value for example) and the script you load will be able to use it, since it runs on the same page.

What do you mean send to script ? Loaded script can access page and all global variables. Just declare global variable and your script can access in regular way.

Related

JavaScript insert script with async and use functions from it? [duplicate]

This question already has answers here:
Accessing variables after dynamically loading a script
(2 answers)
Closed 8 years ago.
I have written a small widget which I include onto pages like this:
<script type="text/javascript">
var _sid = '1';
(function() {
var se = document.createElement('script'); se.type = 'text/javascript'; se.async = true;
se.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://') + 'dev.domain.com/widget/hello.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(se, s);
})();
</script>
Now I want to find a way to call functions that exist within hello.js, which currently looks like this:
var widget = function () {
function setName(a) {
console.log(a);
}
return widget;
}();
So I want to be able to make a call to setName like so: widget.setName("Andy")
...from the embed page, but for some reason I get "widget is undefined."
Any ideas what I am doing wrong?
Before you use widget, you need to know it is loaded, there are multiple options for that:
Use requireJs or something like it.
Implement a callback function in the javascript you are loading, say it will fire 'document.onHelloIsLoaded', then if you want to know if hello is loaded, just set that variable to a function.
Make a loop, checking if the widget variable is set. setInterval (which cancels itself when widget is found and the code has executed)
you also need to make the setName variable accessible when you have access to widget, which it now isn't. You should put something like this:
var widget = {};
widget.setName = function()...
return widget;

Check if javascript is already attached to a page?

Is there any way to check if javascript file is already attached to the page by its file name.
For eg :
if ("path-to-script/scriptname.js") already embeded
{
call related function
}
else
{
Append '<script src="path-to-script/scriptname.js" type="text/javascript"> </script> '
call related function
}
Basically I dont want 1 script to be attached twice on the same page.
You might not always know what objects or functions a script contains in advance, in such cases you can search for script tags containing the desired src.
With jquery:
$("script[src*='"+scriptName+"']");
Without jquery:
document.querySelector("script[src*='"+scriptName+"']");
You'd need to test whether the actual function from the script file exists, like this:
if (window.function_name) {
// script loaded
} else {
// script not loaded
}
I agree with #zathrus though I think you should be using requirejs for things like this. The idea is that dependencies must be fetched before executing the code. The above method you are using may work but you can not guarantee anything.
RequireJS will beautifully maintain all the dependency loading. It is very easy to learn as well.
Simply check if the library is defined, otherwise import it:
if ( !jQuery ) {
var s = document.createElement('script');
s.type = 'text/javascript';
document.body.appendChild(s);
s.src = "path-to-script/scriptname.js";
void(0);
}
// call function
you really need a script loader.. because as you said you want it specific with the javascript resource filename and this is sort of your javascript files are depending to each other
www.headjs.com
www.modernizr.com
www.yepnopejs.com
I thought this will help you.
if (typeof scriptname== "undefined") {
var e = document.createElement("script");
e.src = "scriptname.js";
e.type = "text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);
}

Setting contents of script with jQuery

Is it possible to set the contents of a script element with jQuery? Currently I have:
$(document).ready(function () {
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "http://somedomain.com/somescri...";
$("head").append(s);
});
What if I wanted to replace src property with actual javascript code? Is this possible with jQuery or Javascript?
There's a better way to write that, using $.getScript:
$.getScript("http://somedomain.com/somescri...")
As mentioned, you're looking for eval
eval('x = 2');
alert(x)

How to pass a variable in javascript

I am building a bookmarlet based on this site: http://www.latentmotion.com/how-to-create-a-jquery-bookmarklet/
This is the code of bookmarlet:
javascript:(function(){
var head=document.getElementsByTagName('head')0],
script=document.createElement('script');
script.type='text/javascript';
script.src='http://myserver.com/bookmarlet-remote.js?' + Math.floor(Math.random()*99999);
head.appendChild(script);
})();
void 0
How I can pass a variable from the bookmarlet (above code), to bookmarlet-remote.js ?
I've tried after var myNewvar='myValue', without success, Any Idea?
All JS code on a page (including bookmarklet code and scripts included have) have access to the global scope. If you define a variable without the var prefix it will be available to all other scripts.
It might be a good idea to be explicit about this. do window.myVar = "foo"; to clearly signal that you are working with global variables.
Using var in the function makes it local to that function. To make it global you have to add it to the scope of the window, so:
window.newVariable = window.newVariable || 'Your new value here';
OR
window['newVariable'] = 'Your new value here';
You'd create a public variable.
window.rnd = Math.floor(Math.random()*99999);
In bookmarlet-remote.js you just access the variable.

Can't access variables from dynamically loaded javascript

I'm using a fairly simple system to load javascript dynamically:
include = function (url) {
var e = document.createElement("script");
e.src = url;
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);
};
Let's say I have a file test.js which has the following contents:
var foo = 4;
Now, in my original script, I want to use
include(test.js);
console.log(foo);
However, I get a 'foo has not been defined' error on this. I'm guessing it has to do with the dynamic script being included as the last child of the <head> tag. How can I get this to work?
It is because you have to wait for the script to load. It is not synchronous like you would think. This may work for you:
include = function (url, fn) {
var e = document.createElement("script");
e.onload = fn;
e.src = url;
e.async=true;
document.getElementsByTagName("head")[0].appendChild(e);
};
include("test.js",function(){
console.log(foo);
});
That is one problem, but it also takes time for the browser to download and parse the remote JS file — and it hasn't done that before you call console.log.
You need to delay things until the script has loaded.
This is easiest done by letting a library do the heavy lifting, jQuery has a getScript method that lets you pass a callback to run when the script has loaded.

Categories