.
3 Ways to refresh/reload the page using javascript
- <input type=”button” value=”Reload Page” onClick=”window.location.reload()”>
- <input type=”button” value=”Reload Page” onClick=”history.go(0)”>
- <input type=”button” value=”Reload Page” onClick=”window.location.href=window.location.href”>
Javascript, page refresh
To replace all occurrences in a string use the “g” modifier without double quote like this
var strvar = “one two one”;
strvar = strvar.replace(/one/g,”1″);
the output of the strvar would be “1 two 1″
Javascript, replace
It’s a security thing to prevent people from using AJAX between websites and servers.
Basically, whenever you write a URL in AJAX, it has to be the same URL as the website you are running the script on. Even the www. has to be the same.
Ajax, Javascript
If you’re using the following method to invoke more than one function then you’re stuck, you will be wasting you time on debugging trying to figure out what’s the problem.
-
function callMe1() {
-
….
-
}
-
-
function callMe2() {
-
….
-
}
-
-
window.onload = callMe1;
-
window.onload = callMe2;
callMe2 function will replace callMe1 function, and only callMe2 function will run.
The workaround of this would be using the Anonymous Function
-
window.onload = function() {
-
callMe1();
-
callMe2();
-
}
Another way to overcome this kind of situation is to use the addLoadEvent function
-
function addLoadEvent(func) {
-
var oldonload = window.onload;
-
if (typeof window.onload != 'function') {
-
window.onload = func;
-
} else {
-
window.onload = function() {
-
oldonload();
-
func();
-
}
-
}
-
}
-
-
use this as follows
-
-
addLoadEvent(callMe1);
-
addLoadEvent(callMe2);
function, Javascript
Creating a function in javascript is very simple but calling / invoking the function sometime gets tricky when a function don’t have a parameters, and wondering why the function is it not working….
Example:
-
function countBodyChildren() {
-
var body_element = document.getElementsByTagName("body")[0];
-
-
alert(body_element.childNodes.length);
-
}
and you invoke the function like this
-
window.onload = countBodyChildren();
when you run your program, nothing happens. So, what’s the problem?
In javascript, calling function without parameter should not have an open and close parenthesis, that’s all folk….:)
It should be
-
window.onload = countBodyChildren;
function, Javascript
Function Literal Notation:
var f = function(){ return 1; }
Javascript