JavaScript
JavaScript
How to open a popup window upon page load
Feb 28th
In JavaScript, window.open method can be used to open link in a new window. This method can be used to open a new window when a page is opened. However this method of opening a pop-up window will be blocked by recent browsers as part of their pop-up advertisement blocking feature.
Place the following code in your HTML file.
<script type="text/javascript"><!--
window.open('http://techpulp.com');
//--></script>
View page source with a link or a button using Java Script
Oct 9th
window.location can be set to a special location using view-source: followed by window.location.href to make browser to display source code of the current page. This can be attached to a standard link or a click of a button as shown below.
The following shows how to fire view-source window using a standard link: Place this code between <head> and </head> tags of the page.
<script language=JavaScript>
function viewSource() {
window.location = "view-source:" + window.location.href;
}
</script>
Place this code where you would like your link to appear.
<a href="javascript:viewSource()">View Source</a>
Alternately if button needs to fire the view-source window of the browser, add the following piece of code More >
How to prompt for e-mail on click of a button using Java Script
Oct 9th
In general a standard link can be created to prompt for an e-mail as shown below and it doesn’t involve Java Script.
<a href="mailto:neo@techpulp.com">Mail to Neo</a>
However same functionality can be attached to a button using onClick event of the button as show below. But this requires Java Script support.
<form>
<input type="button" value="Email Neo"
onClick="location.href='mailto:neo@techpulp.com'">
</form>
Moving to previous page in browser using Java Script
Oct 9th
history.go method in Java Script can be used to make the browser go back to previous page in history. This can be invoked using a normal link or on click of a button. The following examples show how to do that.
Go to last page if a link is clicked:
<a href="javascript: history.go(-1)">Back</a>
Go to last page if a button is clicked:
<input type="button" value="Back to Previous Page"
onClick="javascript: history.go(-1)">
How to redirect a page in Java Script
Oct 9th
window.location can set to URL of a page to which redirection is desired. This can be done by placing the Java Script code between head tags. The following example show how a page redirects to http://techpulp.com/newpage.php upon loading. The redirection URL can be changed to any URL.
<head> ... <script language="JavaScript"> window.location="http://techpulp.com/newpage.php"; </script> ... </head>
Reload/Refesh a window in Java Script
Oct 9th
The method window.location.reload() can be used to reload the current browser window in Java Script. This can be invoked on various events like click of a normal link, click of a button etc. The following examples show how to do that.
Reload current window if a link is clicked:
<a href="javascript: window.location.reload()">Reload Window</a>
Reload current window if a button is clicked:
<form method="post"> <input type="button" value="Reload Window" onclick="window.location.reload()"> </form>
How to close a window with a link using JavaScript
Oct 9th
The method self.close can be used to close the current browser window in Java Script. This can be invoked on various events like click of a normal link, click of a button etc. The following examples show how to do that.
Close current window if a link is clicked:
<a href="javascript: self.close()">Close Window</a>
Close current window if a button is clicked:
<form method="post"> <input type="button" value="Close Window" onclick="window.close()"> </form>
How to open a new window on click of a button in Java Script
Oct 9th
In Java Script, window.open method can be used to open link in a new window. This method can used to open a new window when onclick event of the button is fired. The following example shows how a new window can be opened with a URL.
<form>
<input onclick="window.open('http://somesite.com/')"
type="button" value="Open Window" />
</form>


Recent Comments