Problem in my javascript - javascript

The page URL has to change when the loadTest() function is called on a ahref click. But the page is getting refreshed only when alert is included in the code. If I remove the alert the same page is getting refreshed and not the new URL which I need.
<script type="text/javascript">
function loadText(prod_id)
{
curUrl = document.location.href.split("?");
document.location = curUrl[0]+'?prodId='+prod_id;
alert("Test");
}
</script>

use window.location instead of document.location
function loadText(prod_id)
{
curUrl = window.location.split("?");
window.location = curUrl[0]+'?prodId='+prod_id;
}

window.location = curUrl[0]+'?prodId='+prod_id;

Related

How to call a function written in Snippet after redirecting the current window to a link (eg. window.location = "https://google.com")

this.testing = function(link) {
window.location = link;
window.addEventListener('load', function() {
alert("It's loaded!");
});
//OR
myFunctionCall()
}
This is the code I'm trying to implement but after the window is redirected to a link Im not getting the alert.
My aim is to call a function which is dependent on the window after getting redirected

Loading page with pathname when a button is clicked in javascript

I am a little bit confused here.
I have a url locahost/product-location/agro-product and want when a user clicks on a button on this page it takes the user to locahost/product/agro-product. After some research i figured out i could change the pathname this way
<script type="text/javascript">
function loadPage(){
var theURL = window.location.pathname;
return theURL.replace("/product-location/", "/product/");
}
</script>
The above works because if I add this alert(loadPage()); outside the function: it alerts the new URL path.
Now how do I write the code from here so when a user clicks the button it takes the user to the new URL?
You can use window.location.href:
function loadPage(){
var theURL = window.location.pathname;
var newURL = theURL.replace("/product-location/", "/product/");
//Set URL
window.location.href = newURL;
}
OK.got it. Just had to do a little rewriting
function loadPage(){
var theURL = window.location.href;
return window.location = theURL.replace("/product-location/", "/product/");
//Set URL
}
I'd recommend using window.open because you can choose what window it opens in (not to mention a variety of different options).
function loadPage(){
var newURL = window.location.pathname.replace("/product-location/", "/product/");
window.open(newURL);
//LOAD IN NEW WINDOW/TAB INSTEAD:
//window.open(newURL, "_blank");
}
Your button HTML would look like this:
<button onclick="loadPage();">Visit new page</button>

Redirect and Print Page Onload using jquery

I want to redirect(window.location or window.replace) to new url and on loading of that page want to print that page (.print()) using jquery.
I have tried
window.location.replace(url).print();
But it just redirect page and wan't print the page,
Any Idea ?
Add the print() to your onload-function in the site you want to print.
window.location.replace(url)
redirects to a site directly. so print() is never called.
If this is not possible you can use the following code:
function printExternal(url) {
var printWindow = window.open( url, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0');
printWindow.addEventListener('load', function(){
printWindow.print();
printWindow.close();
}, true);
}
In your first file, add following script:
<body>
<a href="yourPageThatYouWantRedirectTo.html" onclick='func()'>Click and redirect</a>
<script>
function func(){
localStorage.setItem('redirected', true);
}
</script>
</body>
And in your another file, which you are redirecting to:
window.onload = function(){
var wasRedirected = JSON.parse(localStorage.getItem('redirected'));
if (wasRedirected == true) {
window.print();
localStorage.clear();
}
}

How to relocate to another page when the user refreshes the browser?

I'm looking to have the following functionality. When the user refreshes the page I want it to go to a certain page Eg:
$(window).bind('beforeunload',function(){
window.location.href = "#splash";
});
Now the .bind() works because if I say return 'Really?'; it opens up a dialog saying Really? But I would like the page to be changed. How would I do this?
i have found next tip (tested in latest chrome):
var flag = true;
function confirmExit()
{
if (flag)
setTimeout(function(){flag=false;location = '/index.html';});
}
window.onbeforeunload = confirmExit;
Try this one.
$(window).bind('beforeunload',function(e){
e.preventDefault();
window.location.href = "#splash";
});
Use:
function confirmExit()
{
alert("exiting");
window.location.href='index.html';
return true;
}
window.onbeforeunload = confirmExit;

Is it possible to navigate to particular URL on window.unload?

Is it possible to navigate to particular URL on window.unload?
Something like below.......target browser is chrome and safari.
$(window).on('beforeunload', function () {
window.location.href = "default.aspx";
});
Add this to the <head> tag.
<script>
window.onbeforeunload = function () {
window.location = 'http://www.example.com';
return false;
}
</script>
Or if you have an external js, that'd do too.

Categories