How to redirect parent page using javascript - javascript

I want to ask you how to redirect parent page to a certain url from a pup up page? I tried several options as parent or opener and nothing is working. Please help me to understand what is wrong.
here is the code i used:
<html>
<head>
<script>
function openWin(){
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>This is 'myWindow'</p> <button onclick=\"window.opener.location.href = ='http://www.google.com'; window.close();\">refresh</button>");
myWindow.focus();
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>

You have an error in your code. Should be
myWindow.document.write("<p>This is 'myWindow'</p> <button onclick=\"window.opener.location.href = 'http://www.google.com'; window.close();\">refresh</button>");
i.e., remove the extra = before 'http://www.google.com'.

Related

how to tell if a popup has fully loaded

Is it possible to tell from a parent window if a popup window has fully loaded?
<body>
<div id="btn">display web page</div>
<div id="somediv">
</div>
</body>
I write the following script to determine if the pop up is already loaded, but it does not work.
<script>
$("#somediv")[0].onload = function(){
alert('loaded');
console.log("rr");
};
</script>
Is this what you want?
<body>
<div id="btn">display web page</div>
<div id="somediv">
</div>
</body>
$("#somediv").load("a.html", function(){
alert('loaded');
console.log("rr");
});
https://jsfiddle.net/jboo92k4/1/
Is this what you were looking for?
<html>
<head>
<script type="text/javascript">
var winPop;
function OpenWindow() {
winPop = window.open("popupwin.html");
CheckWinStatus();
}
function CheckWinStatus() {
try {
asdf = winPop.document.body;
WindowLoaded();
}
catch(e) {
setTimeout("CheckWinStatus()",1000);
}
}
function WindowLoaded() {
alert(winPop.document.title);
}
</script>
</head>
<body>
<form name="Form1">
<input type="button" name="B1" value="Open" onclick="OpenWindow()">
</form>
</body>
</html>
The function OpenWindow() is called when button b1 is clicked.
Inside OpenWindow(), a popup window popupwin.html is made to open.
CheckWinStatus() checks whether the popup has fully loaded or not. It has a timeout value of 1000 ms.
When the popup window has fully loaded, WindowLoaded() is called. This is where you would perform your popup dependent action.

Without alert, my web service wont get called

This is my code where I am trying to call my RESTful web service in dotnetnuke HTML module.
If i remove the alert in javascript and press submit button, the page doesnt redirect. And as I put the alert again, it works! I think it has something to do with the delay. I want to make this work without making use of the alert function. Please help.
<html>
<head>
<script type="text/javascript">
function s()
{
var a=document.getElementById('txt').value;
var url= 'http://localhost:9737/RestServiceImpl.svc/xml/'+a;
window.location = url;
window.alert("hi");
}
</script>
</head>
<body> <input type="text" name="txt" id="txt" />
<input type="submit" value="submit" onclick="s();"/>
</body>
</html>
Try to move your <script type="text/javascript"> just before ending </body> tag.
<html>
<head><title></title> </head>
<body> <input type="text" name="txt" id="txt" />
<input type="submit" value="submit" onclick="s();"/>
<script type="text/javascript">
function s()
{
var a=document.getElementById('txt').value;
var url= 'http://localhost:9737/RestServiceImpl.svc/xml/'+a;
window.location = url;
}
</script>
</body>
</html>
Hope this will resolve your problem.
need to prevent default, otherwise browser immediately navigates away
function s(event) {
var a=document.getElementById('txt').value;
var url= 'http://localhost:9737/RestServiceImpl.svc/xml/'+a;
window.location = url;
event.preventDefault();
}
alternatively you could return false; from function s()

Set zoom level on a new internet explorer child window

I have the below code which launches the page and sets the zoom to 175%. I would like to do this for the w3schools link below...e.g. in a child window if anyone knows how. thanks .
IE 8 compatible, dont care about other browsers at the moment.
<html>
<head>
<title>olecmdid</title>
<script>
function zoom(percent) {
var PROMPT = 1; // 1 PROMPT & 2 DONT PROMPT USER
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
var status = WebBrowser1.QueryStatusWB(63);
document.getElementById("QueryStatusWB_Result").innerHTML = status;
WebBrowser1.ExecWB(63,PROMPT,percent,null);
WebBrowser1.outerHTML = "";
}
</script>
</head>
<body onload="zoom(175);">
<form name="form">
<input type="Button" value="25%" onclick="zoom(25);">
<input type="Button" value="50%" onclick="zoom(50);">
<input type="Button" value="100%" onclick="zoom(100);">
<input type="Button" value="150%" onclick="zoom(150);">
<input type="Button" value="200%" onclick="zoom(200);">
</form>
Visit W3Schools.com!
</body>
</html>
I managed to get a workable solution for what I needed. So I resize the existing window and then launch a new window and then go back and resize the original window. Might help someone else.
<html>
<head>
<meta http-equiv="x-ua-compatible" content="IE=10">
<script>
function openwind() {
zoom(400);
var purchaseWin = window.open("test.html", "Google", "width=600, height=600");
setTimeout(function(){
zoom(100);
}, 500);
}
function zoom(percent) {
document.getElementById('WebBrowser1').ExecWB(63,2,percent,null);
}
</script>
</head>
<body>
<input type="button" value="Open window 400% Zoomed" onclick="openwind()">
<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>
</body>
</html>

Access iFrame URL with JQuery / Javascript

I have a page like this...
<div>
<iframe id="mainContent">
<iframe id="littleBox">
</div>
and I want to do this...
$(".someButton").live('click', function() {
alert(/*The URL That the Main Frame is ON*/);
});
I found this: $("#mainFrame").get(0).location.href, but it doesnt work...
Also, I need it to return the current url, so if someone navigates around it should return the current page they are on.
Okay so you mean you have to access the URL of the parent from the child iframe?
$("#mainFrame").get(0).contentWindow.location
Does iframe.src not work? Or, are you trying to get the parent document? In that case, you can just use parent.
As Azmisov says iframe.src does the trick
<head runat="server">
<title>iframe Test</title>
<script type="text/javascript">
function ShowURLs() {
var control = document.getElementById("mainContent");
alert("iframe source = " + control.src);
alert("this page = " + location.href);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="someButton" value="Click Me" onclick="ShowURLs()" />
<div>
<iframe id="mainContent" src="Test.aspx" />
<iframe id="littleBox" />
</div>
</div>
</form>
</body>
document.getElementById('mainContent').contentWindow.location.href

Newbie: hanging browser on function call

I just started learning JavaScript and am wondering why this simple snippet hangs when I click on the "Call function" button. What am I missing?
<html>
<head>
<script type="text/javascript">
function myfunction()
{
document.write("hello");
}
</script>
</head>
<body>
<form>
<input type="button"
onclick="myfunction()"
value="Call function">
</form>
</body>
</html>
You need to write inside an element or give an element a value, or you should use document write like that :
<html>
<head>
<script type="text/javascript">
function myfunction()
{
document.getElementById("lblMessage").innerText = "hello";
document.getElementById("txtMessage").value = "hello";
//document.write("hello");
}
</script>
</head>
<body>
<form>
<script type="text/javascript">
document.write("This is written while page processed by browser !<br />");
</script>
<input type="text" id="txtMessage" /><br />
<span id="lblMessage"></span><br />
<input type="button"
onclick="myfunction()"
value="Call function">
</form>
</body>
</html>
If you simply want to see your button doing something then try:
alert("Hello");
instead of the document.write.
Where do you expect the function to output its "hello"? Right into the button's source code? That makes no sense. The browser is confused and hangs.
Document.write doesn't magically insert something at the end of your document. It writes its stuff out right there where it is called.
Not too sure what you mean by "hang"... Try this out... The alerts can be removed, but will inform you of where it is at in execution...
<html>
<head>
<script type="text/javascript">
function myFunction() {
//for debugging
alert('Made it into function');
document.getElementById('MyOutputDiv').innerHTML = 'Word To Your Mom...';
//for debugging
alert('function complete');
}
</script>
</head>
<body>
<input type='button' onclick='myFunction();' value='Call Function'>
<br>
<div id='MyOutputDiv'></div>
</body>
</html>
document.write, but where? You have to specify this.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello</title>
</head>
<body>
<pre><script type="text/javascript">
function myfunction() {
d = document.getElementById('hello');
d.style.display = "block";
d = document.getElementById('callme');
d.style.display = "none";
}
</script>
</pre>
<div id="hello" style="display:none">
Hello
</div>
<div id="callme">
<input type="button"
onclick="myfunction()"
value="Call function">
</input>
</div>
</body>
</html>
I can't explain the "hang" but please take a look at this page for a primer on document.write: http://javascript.about.com/library/blwrite.htm I would also like to second that you probably want to write to a specific element in the page rather than just overwriting the entire page.

Categories

Resources