Yogesh's Blog

Sharing me with blog…
Archive for the ‘Javascript’ Category

April 14th, 2016

RegEx to find tags

No Comments, Java, Javascript, by Yogesh.

Starting of Tag: JavaScript: /<([A-Z,a-z,0-9])\w+>/g Java: <([A-Z,a-z,0-9])\\w+> Closing of Tag: JavaScript: /<\/([A-Z,a-z,0-9])\w+>/g Java: </([A-Z,a-z,0-9])\\w+>

If looking simple way to scroll a page to specific part of page i.e. specific DOM element, here is the simple way. Support you have a DOM element like: <span id=”scrollHere”></span> With window load or specific click on button/link, use following javascript statement: Syntex: window.document.hash = “#<d_of_dom_element>”; Example: window.document.hash = “#scrollHere”

Today i faced a problem in opening a new window through javascript’s window.open function. I provided new window url, window title and parameters like height, width etc. This was working fine in other browsers except IE. When window.open call was made, js error was coming and giving invalid argument attribute error. After some investigation, i found the reason was the title i was providing as second parameter in window.open call. Reason of error was space in provided title. So to fix the error, i modified title and replaced space to underscore. To do this, i applied this modification in code: Suppose title i was providing is stores in a javascript variable var title = “hello my dear friend”; var popupUrl = “http://www.yogeshsharma.in”; var otherAttb = “width=500, height=500”; window.open(popupUrl, title.replace(/ /g, ‘_’), otherAttb); Here title.replace(/ /g, ‘_’) replaces all the spaces from title to underscore, then no js comes in IE. 🙂

June 30th, 2010

Prevent browser closing

1 Comment, Javascript, by Yogesh.

Due to some reason,  sometime we want to prevent closing of browser. Actually for security reason, we can not force client to keep browser window open, but what we can do: we can ask client confirmation for it. So to prevent closing of browser window, following javascript code can be used. This works in IE, Mozilla, Safari (Checked with IE6, IE7, IE8, Mozilla firefox 3.5, Safari 5) <SCRIPT LANGUAGE=”JavaScript”> window.onbeforeunload = function(e) { e = e || window.event; var message = “Your message here”; e.returnValue = message; return message; }; </SCRIPT> Here e.returnValue = message; does not work in safari but return message; does. When the alert will come? – On closing browser using cross button (X) – On refreshing browser (browser’s refresh button or F5) – On hitting address bar (either address is changed or not) – Going back (By hiting back button or Backspace) – By clicking link When alert will not come? – By clicking on link to open pop-up window Now here comes the condition that you don’t want to show alert when user clicks on particular link to open some other page in same window. Do by this: <SCRIPT LANGUAGE=”JavaScript”> var showPreventMessage = true; window.onbeforeunload = function(e) { e = e || window.event; if(showPreventMessage){ var message = “Your message here.”; e.returnValue = message; return message; } else { // it means here you don’t want to show message. } }; function disablePreventMessage(){ showPreventMessage = false; } </SCRIPT> Now just call the disablePreventMessage function on the onClick event of link you…