Archive for the ‘Javascript’ Category

Window.open displays the cached version of page

January 29, 2009

Window.open JavaScript function inherently opens the cached version of the page requested. For the first time the web page will be brought from the server, but for subsequent requests it will display the cached version of the page that was bought first. How to avoid this..?

Window.open shows the cached version of the page ONLY if the URL requested matches the cached page URL. The best option is to change the URL every time you try to use the window.open statement. This can be achieved by two methods:

  1. Append a dummy queryString with Random value
  2. Append a dummy queryString with DateTime stamp

Out of the two the second method is most reliable, since the DateTime stamp will be unique every time you invoke the window.open statement. Example:


var dt = new Date().toString();
var URLToOpen = 'http://server.com/page1.aspx?TimeStamp=' + dt;
window.open('New Page','',URLToOpen);

Your comments are welcome.

Advertisements

How to get the position of a control using Javascript?

January 24, 2009

Here is a handy functions to get the posisiton of a control using Javascript:

Usage:

alert(getElementPosition('txtName').top + ' ' + getElementPosition('txtName').left);

Function:

 function getElementPosition(elemID) {
var offsetTrail = document.getElementById(elemID);
var offsetLeft = 0;
var offsetTop = 0;
while (offsetTrail) {
offsetLeft += offsetTrail.offsetLeft;
offsetTop += offsetTrail.offsetTop;
offsetTrail = offsetTrail.offsetParent;
}
return { left: offsetLeft, top: offsetTop };
}

We can get the current scroll position of the document using:

var scrolly = typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement.scrollTop;
var scrollx = typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement.scrollLeft;

Hope this helps you…

Javascript to Bookmark a website!

January 2, 2009

Here is a javascript function to bookmark any website:
Usage:
javascript:bookmark(‘http://sandeep-aparajit.blogspot.com’, ‘Sandeep Aparajit Blogs’);


<script type="text/javascript">
function bookmark(url,title)
{
if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4))
{
window.external.AddFavorite(url,title);
}
else if (navigator.appName == "Netscape")
{
window.sidebar.addPanel(title,url,"");
}
else
{
alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
}
}
</script>

Hope this helps 🙂