Inject JavaScript after refresh - javascript

I would like to my script work after website refresh. I have JavaScript code that is injecting to webbrowser.
string element = "alert(\"hello\")";
form1.webBrowser1.Document.InvokeScript("eval", new object[] { element });

Shamelessly stolen from here:
"You don't want the refresh, you want the onbeforeunload event.
http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx
Sample code from article"
<HTML>
<head>
<script>
function closeIt()
{
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
<a href="http://www.microsoft.com">Click here to navigate to
www.microsoft.com</a>
</body>
</html>

Related

addition Blank window opened with pdf file in javascript

I want to open a pdf file when page is getting loaded (onload), I done it through calling javascript function on onload action ,I can open a pdf in adobe reader ,but issue is I got new Blank browser-window in addition to adobe file(opened in adobe not in browser)
<html>
<script type="text/javascript">
function windoeOpen() {
var myWindow = window.open(" ", "windowname", "width=200, height=100");
myWindow.location.href = "file:///F:/pdf2.pdf";
}
</script>
<body onload="windoeOpen()">
<code>
........
</code>
</body>
</html>
please give me suggestion how can I close the additional browser blank window.
Note:
but if I open pdf in browser ,it works fine(opened in browser as expected) no addition blank browser.
You just need to specify window.location.href like this:
JavaScript:
<script type="text/javascript">
function windoeOpen()
{
window.location.href = "file:///F:/pdf2.pdf";
}
</script>
HTML:
<body onload ="windoeOpen()">

In cordova, how to navigate to another html page?

I am currently working on a Cordova project in Visual Studio. In this project, I am trying building 2 html pages, let me call them first.html and second.html.
In the first.html, I want to add a link to second.html, which allows me to navigate to second.html. I tried 2 ways.
window.location
window.location = "second.html"
tag
<a href=“second.html”></a>
As a result, they both caused an error saying "Exception occurred
Message: Exception: Cannot redefine property: org".
Can anyone tell me how to navigate to a new page properly?
You can navigate to another page using window.location.href. An example is shown below
function(){ window.location.href = "second.html";}
try this it work's for me
<!DOCTYPE HTML>
<html>
<head>
<title>My PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
<script type="text/javascript" charset="utf-8">
function onLoad()
{
document.addEventListener("deviceready", onDeviceReady, true);
}
function onDeviceReady()
{
// navigator.notification.alert("PhoneGap is working");
}
function callAnothePage()
{
window.location = "test.html";
}
</script>
</head>
<body onload="onLoad();">
<h1>Welcome to PhoneGap</h1>
<h2>Edit assets/www/index.html</h2>
<button name="buttonClick" onclick="callAnothePage()">Click Me!</button>
</body>
</html>
You can use the below line to navigate one page to another page.
$('#yourelement').click(function(){
window.location.assign('name_of_page.html');
});
Try this:
window.open("second.html");
window.open opens a new window/tab with the selected URL, while the mentioned method in the question redirects the current page to the selected URL.

Browser close event For Firefox not working

In my application i have to alert user to signout on browser close.
For that i have used the javascript as below
<body scroll="no" onbeforeunload="browerClose()">
<script language="JavaScript" type="text/javascript">
function browerClose()
{
window.alert("Click OK to SignOut");
window.location.href = "http://localhost:8086/egs/ervlet?pri=logOut";
}
this is Working for IE ,but not works for FireFox,
Wats the Problem....Any Suggesstions
Thankxx in Advance,
I would suggest you move the javascript function to the head section of the HTML document. That way it is working for Firefox. Maybe this is because HTML documents are processed in sequential order and you need to define the function before you can use it.
Do something like this:
<head>
<script language="JavaScript" type="text/javascript">
function browerClose()
{
window.alert("Click OK to SignOut");
window.location.href = "http://google.com";
}
</script>
</head>
<body scroll="no" onbeforeunload="browerClose()">
<!-- you code here -->
onbeforeunload event will not work from fire fox version 26 if u used any custom alert message in your application which means you need to perform x operation when closing browser/tab or refreshing page but you put alert message in function which will be called from onbeforeunload then the x (update) operation will not happened.
<html>
<head>
<script>
function unloadfunction() {
alert('test');
// update employee ID in Database
}
</script>
</head>
<body onbeforeunload="unloadfunction();">
</body>
</html>

How to cancel browser close window?

Is it possible to cancel the browser close window event? Or at least add a message prompting the user whether they are sure they want to leave the page? I tried doing this with the beforeunload event, but it still closes the browser. I am looking for something similar to Microsoft's exchange website.
Thanks!
Here is an Html Code:
<html>
<head>
<script language="javascript" type="text/javascript">
window.onbeforeunload = askConfirm;
function askConfirm(){
return "You have unsaved changes.";
}
</script>
</head>
<body>
</body>
</html>
Paste this code into the text area on this page: http://www.htmlpreview.richiebrownlee.com press the button, then exit out of the page that comes up..
a ha!
It appears there's a supported JQuery solution
$(window).unload(function()
{
//...
});
http://www.webhostingtalk.com/showthread.php?t=927093
this should be cross browser supported...hopefully....
window.onbeforeunload
Will work on Safari, IE, Firefox, Chrome, but not Opera.
onbeforeunload should work fine. Bind this on body/window attribute of the html. For more details check this detailed blog post, blog
use onbeforeunload event http://msdn.microsoft.com/en-us/library/ms536907%28VS.85%29.aspx
<HTML>
<head>
<script>
function closeIt()
{
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
<a href="http://www.microsoft.com">Click here to navigate to
www.microsoft.com</a>
</body>
</html>

How to auto start a a href="javascript:void window.open( open loading

click me
How do you auto start this in HTML as onload? Other stuff I researched doesn't work. I only want my website to open up in a new window and has to work with blogspot.
Put it in a script block, not an A-tag.
<script type='text/javascript'>
window.open('website', 'pukarock', 'width=1018, height=715, scrollbars=yes, resizable=yes')
</script>
Don't use href="javascript:void"
It means people without javascript cannot use the link. Use something like this instead.
Click
Then add this to the head of your page
<script>
function openNewWindow(url) {
window.open(url, 'pukarock', 'width=1018, height=715, scrollbars=yes, resizable=yes')
}
</script>
If you want open page after load content, use this:
<!--use some standards of html-–>
<html>
<head>
</head>
<body onload="openSite();">
<script type='text/javascript'>
function openSite()
{
window.open('website','pukarock','width=1018,height=715,scrollbars=yes,resizable=yes');
}
</script>
</body>
</html>
But it could show pop up window warning.

Categories

Resources