Changing window parent url from child not changing complete url - javascript

From my child window Im trying to refresh the parent window. I tried several js functions refresh or change the parent window url and non of them gave me what I needed.
alert(window.opener.location.href); //gave null
//all the functions bellow changed the url to
// http://mydomain/www.google.com
window.opener.location.assign("www.google.com");
window.opener.location.replace("www.google.com");
window.opener.location.href = "www.google.com";
window.opener.location = "www.google.com";
I also tried parent.location.href but didnt change the parent url.
Is there a way I can set the parent url to the desired url? or just let it refresh the page ?Any ideas?
EDIT
I cant post sample code because What Im doing is a bit complicated. Im using a salesforce button that calls a new window (asp.net page). I dont have much access to that page other than the .aspx page. Its a compiled version of a project we bought. In the .aspx page Im trying to call a js function because I dont have access to the code behind of that page. My js function is supposed to refresh the parent page once the user press on either (yes or no).
<cc1:ImageButton ID="btnNo" runat="server" OnClick="btnNo_Click"
OnClientClick="pop()" Text="No" Width="40px" />
<script type="text/javascript" language="JavaScript">
function pop(){
window.opener.location.reload(true);
//window.opener.location.href = "www.google.com";
}
</script>

This should work:
window.opener.location.reload(false);
EDIT
Based on your comments, it sounds like your violating the Same Origin Policy.

Related

i want to pass an asp session to javascript variable

Ok so this is my ASP code of Login page where on success I am creating one of the session which is Session["role"]="user" and i want to pass that value to HiddenField1 and then I am trying to get this value in JavaScript but when i console.log HiddenField value I am always getting null even though I've tried almost all suggested solutions...if anyone can solve the issue then please give a way
Asp code
if (dr.HasRows)
{
if(dr.Read())
{
//Response.Write("<script> alert( 'the user : "+dr.GetValue(8).ToString()+" Successfully Logged in')</script>");
Session["username"]=dr.GetValue(8).ToString();
Session["fullname"] = dr.GetValue(0).ToString();
Session["role"] = "user";
HiddenField1.Value = Session["role"].ToString();
Response.Write("<script> alert("+HiddenField1.Value+")</script>");
Session["status"] = dr.GetValue(10).ToString();
}
ScriptManager.RegisterStartupScript(this, GetType(), "popup", "MyAlertFunOnSucc()", true);
Response.Redirect("homePage.aspx");
}
JAVASCRIPT in the masterpage Head
<script type="text/javascript">
var array_store;
array_store = document.getElementById("HiddenField1").value;
console.log(array_store);
</script>
And this is my hiddenField
<asp:HiddenField ID="HiddenField1" runat="server" />
and this the ERROR
homePage.aspx:28 Uncaught TypeError: Cannot read properties of null (reading 'value')
Ok, so you might have spend some time making it CRYSTAL clear that you have a master page here?
however, you have this code:
HiddenField1.Value = Session["role"].ToString();
Response.Write("<script> alert("+HiddenField1.Value+")</script>");
Session["status"] = dr.GetValue(10).ToString();
}
ScriptManager.RegisterStartupScript(this, GetType(), "popup", "MyAlertFunOnSucc()", true);
Response.Redirect("homePage.aspx");
You do realize what a response redirect does, right? It blows out the current page, the current class, and the current controls. You then say please forget about this page, and now re-direct to a WHOLE NEW page. (home page).
So, how can you inject a script, but THEN decide to dump the whole current page not send it to the client, and THEN re-direct to a whole different page????
What this means is that you need to put/place/have that code that sets the hidden control, and the register script MOVED to the home page code. So, on home page (is PostBack = false), you can set the hidden field, and THEN do your register script.
but, with your current setup, by executing a redirect to a whole new page, your register script, and in fact even your setting of the hidden field - both make no sense at all if after setting the hidden field, and tossing in a scripts to run?
YOU THEN SAY FORGET about all that and now transfer TRANSFER to a whole new and different page.
So, move your code that sets the hidden field, and register script to the load of the home page (page load event).
You can use a handler (.ashx file) for returning session value and then a ajax call in the front side to get that value , or a method with [WebMethod] attribute in backend side and a ajax call at front side to get session value.

Javascript From Console - Load a Few Pages, Then Run Function Per Page Load [duplicate]

I'd like to be able to call a jquery function once window.location has completed loading a URL. Is this possible? I can't seem to find anything online about this.
for instance:
if(parseInt(msg.status)==1) {
window.location=msg.txt;
alert("This URL has finished loading")
}
Thanks,
-Paul
You can either use window.onload of the destination page (if you have access to modify the code of that page), or you can use window.onunload to have the alert be launched when unloading the current page. You cannot execute code on the current page after the new page has been loaded.
Yes.
This page demonstrates onload/onunload behavior.
<html>
<head>
<script>
window.doUnload = function(){
alert("Here!");
}
window.doLoad = function(){
window.location="http://www.google.com";
}
</script>
</head>
<body onload="doLoad();" onunload="doUnload();"></body>
</html>
After a user logs in for the first time I need to load my index page to initialize everything but then need to forward them to another page for profile completion.
use window.location to redirect the user to your index, adding a query parameter (something like window.location=index.php?firstLogin=true ) and on your index redirect (using javascipt http 300, header() or whatever you are using) to the profile page after it ends loading if the parameter is set
Iframe
One (ugly) method you could use is to instead of using window.location, clearing the body, adding an iframe with the relevant path and listening to its onload function.
After that you can run code inside the iframe as long as it's not cross-site scripting.
I use this method to perform small automated scripts, that can't really on third-party plugins.
Ajax
Another method might be using ajax to load the page/body content. Then replacing your body with the newly loaded body and start executing the next functions.

How to do some operation after web another page is loaded?

Hello I have some JavaScript code to fill some table of page1 and auto click its submit button, then it jump to another page2.
What I want is to execute some other codes after page2 is fully loaded. I know methods like window.onload and jQuery.ready but i don't know how to set this method to page2. For example if i write window.onload then the window reference to the current page.
Can anyone help?
You could achieve it the following way.
example :
Add unique identification classes to each of the page body.
page 1
<body class='page1'></body>
page 2
<body class='page2'></body>
JavaScript
in your javascript file, use the following to run the code.
if($('body').hasClass('page1')){
//run page 1 code
}else{ // You can add a second if just to check if it's page 2
//run page 2 code
}
Then import the file to both pages at the bottom of the body tag. the script will run as needed.
Update
To get the current browser url just use window.location.href to get the url. The url is a unique page identifier so just update the condition to the following.
if(window.location.href == 'page1-url'){
//run page 1 code
}else{ // You can add a second if just to check if it's page 2
//run page 2 code
}
I would prefer using the window.location.pathname since it would just return the page path without the host, but the above will work too.
You can use $( document ).ready() if page 2 is a new document.
If "page 2" is at a scroll point in the same document, you can use an if statement with the scroll point using .scroll(). Add your script in the head of your new document or a link to a new js file.

Is there a way to find the window of the previous page (other tab)?

If you open a link in a new tab (with MMB or right click or preferred method, however), is there a way from the new tab window to find the opener window? These don't work:
window.opener
window.parent
Maybe it's just not possible, since following a link usually 'overwrites' the window, so no ref is possible, but since we have tabs... maybe... a ref IS possible?
I want the opened window to do a postMessage to the original window to let it know the page has loaded, so the original window can update the link style.
I don't make these pages, so I don't have access to the back-end or HTML printage. Think Greasemonkey: I add JS to existing websites to improve them.
The only thing I can think of is intercepting the click event, grabbing the URL, and passing it to window.open and subsequent postMessage calls on the returned window object. Do you generate the page with these links?
In some browsers window.open(url,'_blank') will open in a tab.
I think there is two solutions to your problem
First solution : Cookies
An easy way is to set a cookie with the value of the current page everytime a link is clicked, you can use a library like this one to set a cookie and get it in the next page once you click like this
$("a").click( function () {
$.setCookie("currentPage",document.URL);
});
In the next page you just get the cookie like this
if($.getCookie("currentPage") != undefined )
var previousPage = $.getCookie("currentPage");
Second solution : Server-side langage
If you're using a server side langage like php, it's easy to get the url of the previous page :
<?php
if (isset($_SERVER['HTTP_REFERER']))
echo "<script> var previousPage='" . $_SERVER['HTTP_REFERER'] . "'; </script>";
?>
And finally, to post a message you just need to test if the previousPage variable is set:
if(previousPage != undefined)
{
// Code to post your message
}

Parent Jsp page refresh when click on popupwindow alert box

I am working on jsp's, I have two jsp one in configDb.jsp, in that I have written code to retrieve the values from database and display it. In this whereas I have option like newconnection.. its a popup window. When I click on that it opens popupwindow and taken the values and store them in a database, but in my parent page I am not able to display those values. After I click on the ok button in popup window, I have to refresh the parent page, then I am able to see the values which I have created few seconds back by the new connection page. Could anyone please help me out?
You can include this in your HTML for the popup window.
<script type="text/javascript">
function proceed(){
opener.location.reload(true);
self.close();
}
</script>
<form onsubmit="proceed()">
...
</form>
Actually you can obtain the answer by googling "javascript refresh parent page from popup" and you will see stackoverflow's answer :)
A better alternative without the need of refreshing the parent page can be achieved by adding a submit button listener and perform a DOM action to insert the new element with new content. This can be easily done by Javascript.

Categories

Resources