I have written following function to call another page when i will click print button. but when i click on print button it opens current page instead of the one which i want to call.
function printme(tripId, requestId, Ecommerce) {
//alert("print is called");
alert(tripId);
alert(requestId);
alert(Ecommerce);
window.location.href = "ViewTripConformationPrint.cshtml?";
window.print();
//workaround for Chrome bug - https://code.google.com/p/chromium/issues/detail?id=141633
if (window.stop) {
location.reload(); //triggering unload (e.g. reloading the page) makes the print dialog appear
window.stop(); //immediately stop reloading
}
return false;
}
The problem you're having is that the code after you set window.location.href is executed immediately. The page isn't loaded and then the code continues... you code continues until the end of the function and then window.location.href is actually loaded and processed. If you see what I mean.
You could try moving your code (window.print, if window.stop, etc) into ViewTripConformationPrint.cshtml (as part of onload).
Related
The id's #switchtopagetwo and #switchtoindex are assigned to buttons that do what you can infer from the id's names. What I want to do is on click of the button, I want to redirect to the new page via window.location = url; and then run a function that renders some data on the page via pagetwoData() or pageoneData(), depending on where I am at the moment.
$('#switchtopagetwo').on('click', function () {
window.location = 'pagetwo.html';
pagetwoData();
});
//pagetwo.html button
$('#switchtoindex').on('click', function () {
window.location = 'index.html';
pageoneData();
});
When I comment out window.location, the functions run and I can see the data on the screen, but there's no page redirect even on clicking the button. When I click on the buttons fast enough, I can see the function's data being rendered for a split second and then disappearing. When I console.log certain items, I can see the console.log's appearing in the console and then disappearing the same way.
Clearly there is an issue with window.location. Is there better code I can use for clicking the button, redirecting the page to load the page-2 data, then clicking the button again to go back to page-1 data?
When you redirect to a new page, the entire page context is abandoned and replaced by the new page. Nothing which happens on the source page after that redirect can be relied upon to still happen. But anything on the target page that's loading will happen.
Instead of trying to get Page1 to tell Page2 to do something when it loads, just have that something happen on Page2. For example:
// on index.html
pageoneData();
$('#switchtopagetwo').on('click', function () {
window.location = 'pagetwo.html';
});
// on pagetwo.html
pagetwoData();
$('#switchtoindex').on('click', function () {
window.location = 'index.html';
});
Basically, for any given page, whatever you want to happen on that page when it loads should be executed on that page when it loads.
I'm having a problem always when I try to use the following code in a button in my HTML file.
onClick=window.location.reload();
mapGenerator();
The page reloads but the javascript (mapGenerator) that make a D3JS view doesn't appear. What am I doing wrong?
location.reload() will immediately reload the page and prevent any following code to execute.
You can, however, create a function that executes your method after the page has (re)loaded:
window.onload = function() {
mapGenerator();
};
This method will run every time the page has fully loaded. To only run the code after you have reloaded the page using location.reload(), you could create a method that handles the click by setting a cookie and then reloading the page.
function handleClick() {
document.cookie="reload=true";
location.reload();
}
This would require you to change your onClick value to onClick="handleClick();". Now, whenever the page loads, you can check whether the cookie has been set. Your window.onload function now changes to this:
window.onload = function() {
if(document.cookie.indexOf("reload") >= 0) {
mapGenerator();
}
}
Checking if a cookie exists - answer by Michael Berkowski
After the reload it's up to you whether you want to unset the cookie — if you don't, the page will run the function mapGenerator on every page load until the cookie expires.
If you need more help with cookies, check out W3Schools' tutorial.
As per your description mentioned above two actions are to be taken on click. As the first action reloads the page the second action is lost. If you want any action to be taken on load of the page, mention the same on onload event of the page.
i want to close pop up and refresh parent but after few seconds delay to make sure all data is received. currently i have the following
function closeAndRefresh(){
setTimeout(function()
{
window.open('/{!currSubId}','_top');
window.location = window.location.href;
return true;
},5000)}
This code refreshes the parent but inside the popup, so the pop up does not close, rather the parent shows refreshed - in pop up
So move the pop up outside of the timeout, give it a name, call close, and reload.
function closeAndRefresh(){
var winPop = window.open('/{!currSubId}','_top'); //load pop up give it a name
window.setTimeout(function(){
winPop.close(); //close pop up
window.location.reload(true); //refresh current page
},5000);
}
You could just use an Ajax call to the server and not have to deal with a pop up and wondering if the pop up blocker will block it or not knowing if the call completetd. When the call comes back from the call, you can just reload the page.
So I want to make a script like this:
window.location = "http://m.roblox.com/Catalog/VerifyPurchase?assetid=122174821&type=robux&expectedPrice=1"
document.getElementsByClassName('buyButtonClass')[1].click()
but I don't know how to make the page refresh and the code start over without it having to manually be entered again
Thanks
By the way it will be running in Google Chrome Dev. tools Console
I tried
function blah() {
// window.location = "http://m.roblox.com/Catalog/VerifyPurchase?
assetid=122174821&type=robux&expectedPrice=1"
document.getElementsByClassName('buyButtonClass')[1].click()
if (some_condition) {
blah() // rerun the code
}
}
Output was "undefined", the script did nothing.
The script goes to a link, clicks a button (currently it doesn't click for some reason) then restarts the script (not working)
setting window.location = ... will refresh the page, but stuff after that will not trigger, because you just refreshed the page including all the javascript. You can put the code you want to trigger in a $(document).ready(function(){your_code_here}); call and when the page refreshes, it will set up your click event.
I have a link in an update panel which calls a js method to do printing of the current window:
Print Coupon
where the js method is called on Page_Load event:
private void loadJs()
{
String flashMap = "script";
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.Page.GetType(), flashMap))
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), flashMap, "function print(){ alert('test'); window.print(); return false; }", true);
}
}
When pressing the link, the alert window is shown multiple times (after I click "Ok", which seems strange) but window.print() is never called (a new tab for printing is not opened).
If I directly call javascript:window.link from href it works, but because the link is contained in an update panel, it no longer works second time (that's why I tried to register the script).
Can anybody see the issue here?
I believe window.print(); is actually calling your print() function recursively. Try changing the name of your js function.