I'm building a site and I have loads of scripts going on there, theyre all contained in different files and some of them are onload functions. I know about using a function to run all of them. However this doesn't work so well with multiple files. I've tried using an onload.js file:
window.onload = function() {
// Site wide funcs
searchShow();
// Page specific
if(getProducts && loadMore){
getProducts();
loadMore();
}
if(checkBox){
checkBox();
}
if(styleProduct) {
styleProduct();
}
}
where it should check if the function exists on the page before running it. (Some files are referenced in each file some are site wide and are referenced in the header file)
Could anyone suggest a better option for having all these onload files?
In your script files stick to addEventListener or attachEvent, as both allow multiple handlers for single event.
EDIT
As these two functions are browser specific, it is a good idea to start with some kind of wrapper:
function createEvent(objectTarget, eventName, eventHandler)
{
if (objectTarget.addEventListener)
objectTarget.addEventListener(eventName,eventHandler,false);
else if (objectTarget.attachEvent)
objectTarget.attachEvent('on'+eventName,genericHandler);
}
Now use createEvent to attach new handler to specific event.
createEvent(window, 'load', handler1);
createEvent(window, 'load', handler2);
...
Please take into account, that there are more severe differences in event model between browser (such as where is event object or target vs. srcElement).
Related
I have a web app that uses multiple JavaScript files. I have two event handlers like this in two files:
window.onkeyup = function (e) { SomeFunction1(e) }
window.onkeyup = function (e) { SomeFunction2(e) }
SomeFunction1 is in a file that's used in every part of the site but SomeFunction2 is in a file that's only used in one part of the site.
The problem is that when I include the JavaScript file that contains the handler for SomeFunction2, it replaces the handler for SomeFunction1, which means the event doesn't trigger any more.
Instead of replacing the window.onkeyup handler, I want to add another handler in the javascript file that contains SomeFunction2 so that both functions are called. How can I do that? jQuery available.
You can use on event function for this:
$(window).on('onkeyup', SomeFunction1);
$(window).on('onkeyup', SomeFunction2);
Or using vanilla JS
window.addEventListener('onkeyup', SomeFunction1);
window.addEventListener('onkeyup', SomeFunction2);
If jQuery available, you could easily add as many listener as you want
$(window).on("keyup", function(){alert(1);});
$(window).on("keyup", function(){alert(2);});
Try it on https://jsfiddle.net/45zf9vkd/
That's because you're using the DOM attribute to attach the handler, and there can only be one.
You're better off using the addEventHandler API:
document.querySelector('foo').addEventListener('click', function(){
// Your code goes here
})
OR
document.querySelector('foo').addEventListener('click', someFunction);
function someFunction(){
// Your code goes here
}
You can add as many as you want.
Try this ;)
Add this in a common file:
window.onkeyup = function(e){
try{
SomeFunction1(e);
}catch(e){}
try{
SomeFunction2(e);
}catch(e){}
}
And include your files where SomeFunction1(e) and SomeFunction2(e) where you need them?
You will need to use addEventListener to have multiple handlers for a DOM element e.g.:
document.querySelector('#elementID').addEventListener('keyup', function(){...
If you already have jQuery set up (it is not worth adding jQuery to your site just for this), you can use
$(window).on('keyup', function(){...
This will only add additional handlers and won't overwrite any existing ones
I'm coding a script that will be used on several websites as a plugin. I have to use only Javascript, no framework. I'm asking myself, what is the best way to load my script without causing any trouble with other scripts or frameworks that can be loaded on these websites.
I thought to do a global function where i call the functions I want to use, and I put this function on the window.load event, like this :
<script>
var global = function(){
object.domain.function1();
object.domain.function2(param1, param2);
object.domain.function3(1);
}
(function(){
if(document.addEventListener){
document.addEventListener('load',global, false);
}
if(document.attachEvent){
document.attachEvent('onload',global);
}
})();
</script>
Or simply :
<script>
object.domain.function1();
object.domain.function2(param1, param2);
object.domain.function3(1);
</script>
Of course, I need some elements are loaded on my page.
Thanks in advance.
If you're ok with putting javascript inside the <body> tag then a fairly reliable way of running javascript after the entire page has loaded is placing your javascript in <script> tags at the very bottom of the page.
It's not my preferred way of handling this issue, but if using the onload event is not working quite right for you this would be a good next step.
what is the best way to load my script without causing any trouble with other scripts or frameworks
The things you need to consider are, are you..
polluting the namespace
obstructing another script
The second point is easy to work around, .addEventListener supports adding many listeners for the same event which won't overwrite each other. There are two different events you might be interested in, 'load' on window and 'DOMContentLoaded' on document. You can always put a <script> at the very end of <body> but I personally don't like doing this because you're mixing HTML with JavaScript or changing the DOM tree.
For the first point, this can be done via the use of anonymous functions and, should you need to occupy the namespace, keeping all of your variables and methods inside one object to minimise your impact.
Putting all this together you may end up with something like this
var myObjectName = { // everything in one object in global namespace
domain: {
function1: function () { /*...*/ },
function2: function () { /*...*/ },
function3: function () { /*...*/ }
}
};
(function (fn) {
if (window.addEventListener) {
window.addEventListener('load', fn, false);
}
else if (window.attachEvent) { // Consider if support for `.attachEvent` is really necessary, people need to move on.
window.attachEvent('onload', fn);
}
})(function () { // pass your function anonymously
myObjectName.domain.function1();
myObjectName.domain.function2(param1, param2);
myObjectName.domain.function3(1);
});
I'm trying to work with Raphael for some SVG stuff and tried, well, with my limited knowledge, to build something beautiful ;)
I have 3 files:
1x html file and 2xjs files
html file: with an onload function ( + header,body and stuff)
window.onload=function()
{
init();
}
js File1: has the init function and a function to load js files (e.g. Raphael) and a callback to proceed after the file is loaded
function init()
{
getScripts(initTool)
}
function getScripts(callback)
{
$.when($.getScript(scripts[raphael]).then(callback)
}
function initTool()
{
$('body').append("<div id='tool'></div>");
tool=Raphael("tool",5000,5000);
$('body').append("<a href='javascript:void(0)' onclick='newElement'>New element</a>")
}
js File2: Here I have the function newElement which should add (for this example) a single path to the svg element created by Rapahel
function newElement()
{
tool.path("M10,20L30,40");
}
Unfortunately the path does not show up and I have no idea why. I tried referencing the "tool" variable before the onload in case it it related to global/local variables (wild guessing) but this also does not work. changing id's to "tool" to "tool2" for the svg element also does not work.
What else could it be? Where is my (possibly obvious) blind spot?
SHould callback not be declared as a parameter here?
function getScripts(callback)
{
$.when($.getScript(scripts[raphael]).then(callback)
}
To be honest with you I've written quite a bit of javascript and I don't quite grok variables scopes fully yet. However, when calling functions you should use parenthesis to indicate that it should be executed (there are a couple of times when you reference them without parenthesis, but that is beyond the scope of this answer).
So...
$('body').append("<a href='javascript:void(0)' onclick='newElement()'>New element</a>")
But this isn't enough to make it work, you should also declare your function like this:
var newElement = function() {
tool.path("M10,20L30,40");
}
Here is a working solution: http://jsfiddle.net/vAjG2/
(perhaps somebody can expand on why these changes are needed, I don't grasp them myself).
The problem has nothing to do with variable scope. You just need parentheses following the function name in your inline event handler. Rewrite the last line as:
$('body').append("New element")
and you'll be up and running.
However, inline event handlers are frowned upon for a whole variety of reasons. As quirksmode says: "Although the inline event registration model is ancient and reliable, it has one serious drawback. It requires you to write JavaScript behavior code in your XHTML structure layer, where it doesn't belong."
A much cleaner way to do this would separate out the markup and the script, e.g.:
<div id='tool'></div>
<a id="mylink" href='#'>New element</a>
<script>
var tool = Raphael("tool",500,500);
$('#mylink').on("click", function() {
tool.path("M10,20L30,40");
});
</script>
See this jsfiddle for this code in action.
Lastly, as a helpful hint, I would advise running your code on document ready, instead of window load, especially you're using jquery,. Document ready happens when the DOM is first constructed. Window load waits for all assets to be fully loaded, which can take awhile, and typically isn't necessary. It's long considered a best practice.
Regarding this SO article and this SO article. I looked at these after noticing my web app does not fire in IE8...I don't care about backward compatibility at the moment but if it's one line of code why not? Anyways the other issue I was having is the onload event waits for all the content to load...so the user has no controls if he/she is waiting for images to download. This led me to believe that I should just use
<script type='text/javascript'>my_initialize_function()</script>
placed in the html where I want the page to initialize.
and say to bye to
window.onload = initializePage;
or
window.addEventListener('load',initialize_page);
or any similar.
My question is: Is this a valid approach to initializing one's page?
PS: I'm not using any libraries including JQuery...and obviously I would not try to initialize elements that have not been loaded yet.
No.
jQuery and similar libraries has an interesting approach. They simply capture different events in a crossbrowser manner while making it easier for the developer to use.
Let's consider the following code:
<script> document.getElementById('a').innerHTML='b'; </script>
<div id="a"></div>
It may or may not work depending on whether the browser runs javascript when it finds it or only after the whole document has been built.
On the other hand, if you used the proper event mechanism but the document has already been built, your code will not be called.
jQuery unites both paradigms to get a seamless and better system. Something like so:
var foo = {
// holds list of callbacks to call when document starts
stack: [],
// document not ready yet
ready: false,
// add callback to be called when document is ready
add: function(callback){
foo.stack.push(callback);
if(foo.ready)foo.fire(); // document is ready, call fire
},
// fire callbacks (document IS ready)
fire: function(){
for(var i=0; i<foo.stack.length; i++)
foo.stack[i]();
foo.stack = [];
foo.ready = true; // document IS ready
}
};
// bind to browser events
window.onload = foo.fire; // TODO: you should use actual events
// example of use
foo.add(function(){
alert('Document is ready!');
});
You could use jQuery's $(document).ready() event. It fires after the DOM is complete, but before all images are loaded.
I develop Joomla websites/components/modules and plugins and every so often I require the ability to use JavaScript that triggers an event when the page is loaded. Most of the time this is done using the window.onload function.
My question is:
Is this the best way to trigger JavaScript events on the page loading or is there a better/newer way?
If this is the only way to trigger an event on the page loading, what is the best way to make sure that multiple events can be run by different scripts?
window.onload = function(){}; works, but as you might have noticed, it allows you to specify only 1 listener.
I'd say the better/newer way of doing this would be to use a framework, or to just to use a simple implementation of the native addEventListener and attachEvent (for IE) methods, which allows you to remove the listeners for the events as well.
Here's a cross-browser implementation:
// Cross-browser implementation of element.addEventListener()
function listen(evnt, elem, func) {
if (elem.addEventListener) // W3C DOM
elem.addEventListener(evnt,func,false);
else if (elem.attachEvent) { // IE DOM
var r = elem.attachEvent("on"+evnt, func);
return r;
}
else window.alert('I\'m sorry Dave, I\'m afraid I can\'t do that.');
}
// Use: listen("event name", elem, func);
For the window.onload case use: listen("load", window, function() { });
EDIT I'd like to expand my answer by adding precious information that was pointed by others.
This is about the DOMContentLoaded (Mozilla, Opera and webkit nightlies currently support this) and the onreadystatechange (for IE) events which can be applied to the document object to understand when the document is available to be manipulated (without waiting for all the images/stylesheets etc.. to be loaded).
There are a lot of "hacky" implementations for cross-browsers support of this, so I strongly suggest to use a framework for this feature.
The window.onload events are overridden on multiple creations. To append functions use the
window.addEventListener(W3C standard) or the window.attachEvent(for IE). Use the following code which worked.
if (window.addEventListener) // W3C standard
{
window.addEventListener('load', myFunction, false); // NB **not** 'onload'
}
else if (window.attachEvent) // Microsoft
{
window.attachEvent('onload', myFunction);
}
Modern javascript frameworks have introduced the idea of a "document ready" event. This is an event that will fire when the document is ready to have DOM manipulations performed on it. The "onload" event fires only after EVERYTHING on the page has loaded.
Along with the "document ready" event, the frameworks have provided a way to queue up multiple bits of Javascript code and functions to run when the event fires.
So, if you're opposed to frameworks, the best way to go about this is to implement your own document.onload queue.
If you're not opposed to frameworks, then you'll want to look into jQuery and document.ready, Prototype and dom:loaded, Dojo and addOnLoad or Google for [your framework] and "document ready",.
If you haven't picked a framework but are interested, jQuery is a good place to start. It doesn't change any of Javascript's core functionality, and will generally stay out of your way and let you do things as you like when you want to.
Joomla ships with MooTools, so you'll find it easiest to use the MooTools library for your additional code. MooTools ships with a custom event called domready that fires when the page is loaded and the document tree is parsed.
window.addEvent( domready, function() { code to execute on load here } );
More information about MooTools can be found here. Joomla 1.5 currently ships with MT1.1 while the Joomla 1.6 alpha will include MT1.2
Personally, I prefer this method. Not only does it allow you to have multiple onload functions, but if some script had defined it before you got to it, this method is nice enough to handle that... The only problem left is if a page loads several script which does not use the window.addLoad() function; but that is their problem :).
P.S. Its also great if you want to "chain" more functions later on.
This is the dirty but shorter way :P
function load(){
*code*
}
window[ addEventListener ? 'addEventListener' : 'attachEvent' ]
( addEventListener ? 'load' : 'onload', function(){} )
As of I always include jQuery/bootstrap JS files on bottom of document and have no acces to $ over the document, I developed a tiny "init" plugin which is one and only JS script I include on very top of my pages:
window.init = new function() {
var list = [];
this.push = function(callback) {
if (typeof window.jQuery !== "undefined") {
callback();
} else {
list.push(callback);
}
};
this.run = function() {
if (typeof window.jQuery !== "undefined") {
for(var i in list) {
try {
list[i]();
} catch (ex) {
console.error(ex);
}
}
list = [];
}
};
if (window.addEventListener) {
window.addEventListener("load", this.run, false);
} else if (window.attachEvent) {
window.attachEvent("onload", this.run);
} else {
if (window.onload && window.onload !== this.run) {
this.push(window.onload);
}
window.onload = this.run;
}
};
Using this I can define any anonymous loader over the page, before jQuery and bootstrap inclusion and stay sure that it will fire once jQuery is present:
init.push(function() {
$('[name="date"]').datepicker({
endDate: '0d',
format: 'yyyy-mm-dd',
autoclose: true
}).on('hide', function() {
// $.ajax
});
$('[name="keyword_id"]').select2();
});