How to redirect without using window.location - javascript

If I click a button from page A, the browser will redirect to page B. In page B if I click a another button again it redirects to Page A. Here I used window.location.href to redirect the new page.
eg:window.location.href="http:\\localhost:12345\index2.html"
Is any other alternative way to redirect next page. I don't want to use windows.location
Update:
If I use windows.location the url which I come from is stored in document.reffer. For security purposes I don't want to allow to store the url.

Location assign() Method
You may use this method
example :-
function myFunction() {
location.assign("https://www.google.co.in");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Load new document</button>
</body>
</html>

You can also use
window.location.replace("http://someUrl.com");
replace() does not keep the originating page in the session history.

Using $location in angularjs : See the documentation https://docs.angularjs.org/api/ng/service/$location

$url =$('<a/>')
$url.attr('href',"http://google.com");
$url[0].click()
You can try this.

You can use location.assign() for redirect another page.
location.assign("https://www.facebook.com/");

why not use <a>. what you only to do is give it a class like button.

Related

Auto Click on hyperlink

I would like to a HTML code with the following functions if you guys can help , please.
Once the page loads, after 1 second to auto click on a hyperlinked text , how do I do this ? For the new website to be opened in the same window.
I have this code but this one is sending me to a new window and the pop up is blocking - not good
<!DOCTYPE html>
<html>
<body>
<head>
<script>
function autoClick(){
document.getElementById('linkToClick').click();
}
</script>
</head>
<body onload="setTimeout('autoClick();',700);">
<a id="linkToClick" href="http://www.google.com" target="_blank">GOOGLE</a>
</body>
</body>
</html>
Thanks
Instead of using <a href=> you should try just setting the variable window.location.href. So your code should look something like this:
<body onload="window.location.href='http://www.google.com',700);">
https://www.w3schools.com/howto/howto_js_redirect_webpage.asp
Let me preface this by saying this is extremely questionable when it comes to web design, and generally regarded as a shady or bad practice. That said:
$(window).on('load', function(){
window.location = 'http://example.com'
});
OR (vanilla JS)
window.onload = function(){ window.location = 'http://example.com' };
Again, be careful with this. If you want to do something like this, you need a pretty good reason why.
Possible duplicates:
Redirect from an HTML page
HTML redirect on page load

sessionStorage troubles

I'm trying to understand how sessionStorage works. I get how to make it store some variable and restore it but I wanted to do something different.
This link from w3schools shows how to show/hide elements with a function. I have a pretty similar setup on my site. So I wanted to make it work on the w3schools example then I'll know how to apply it on my site.
Could you perhaps edit the w3schools code to make it use session storage to restore the last option (either shown or hidden) so that I can undestand how it works and apply it then myself?
Thanks in advance.
There you go:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>
<p id="hideMe">some text</p>
<button id="hide">hide</button>
<button id="show">show</button>
<script>
$(function(){
$("#hide").click(function(){
$("#hideMe").hide()
sessionStorage.setItem('hidden', 'true');
})
$("#show").click(function(){
$("#hideMe").show()
sessionStorage.setItem('hidden', 'false');
})
sessionStorage.getItem('hidden') === 'true' ? $("#hideMe").hide() : null
})
</script>
</body>
</html>
(fiddle)
But I'd say it's not what you're looking for. The sessionStorage will survive page reloads but as soon as you close the tab/window it's gone.
The sessionStorage property allows you to access a session Storage object. sessionStorage is similar to Window.localStorage, the only difference is while data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.
(https://developer.mozilla.org/de/docs/Web/API/Window/sessionStorage)

Chrome- Go back to previous webpage automatically?

Is there a way to configure in Chrome (or any browser) that whenever a webpage changes it automatically goes back to the previous page.
E.g. If a user presses a "Submit" button on a survey, they will be shown the "finish" message but then the original webpage with the survey will load again?
To go to previous page use
window.history.go(-1);
Example
<!DOCTYPE html>
<head>
<script>
function initialize(){
var submitBtn = document.getElementById("submitBtn");
submitBtn.addEventListener("click",function(){
alert("successfully submited");
setTimeout(goBack,2000)
});
function goBack(){
window.history.go(-1);
}
}
window.addEventListener("load",initialize);
</script>
</head>
<body>
<button id="submitBtn">Submit</button>
</body>
To go to a specific page
window.location = "";
While I'm not convinced it's a great idea, you know your requirements better than I do.
So to go back, you run this javascript.
window.history.back()
When you say immediately go back, I assume you want them to see the finish message first before being sent back after, say, 3 seconds?
<script>
setTimeout(function() {
window.history.back();
}, 3000);
</script>
Edit
If you want to redirect without javascript, you can use a meta tag in the header
<meta http-equiv="refresh" content="3; url=http://www.example.com" />
So if you use server side rendering you could reference the HTTP_REFERER header and inject it into your meta tag.
If you don't use server side rendering (PHP, MVC, React, etc) and you can't use javascript, then no; you're stuffed.

Auto redirect users to the previous page

I want to redirect users to the previous page whenever they visit a specific page
Let's say: page123.php
I have the following code on that page that redirect users to the previous page but on click:
<script language="javascript">
$(document).ready(function() {
$('.goback').click(function() {
history.go(-1);
});
});
</script>
I want to automate the process so whenever they visit that page (page123.php) they redirected back to the previous one. I'd greatly appreciate it if anyone could help me in achieving this. Thank you.
Note: I don't want to use htaccess redirection.
You can add this tag to your html:
<meta http-equiv="refresh" content="1;url=yoururlhere" />

Fetching page source

I have the following code in my page
<html>
<head>
<title>testpage</title>
<script language = 'javascript'>function fchange(){alert(document.getElementById("ifrm").value);</script>
</head>
<body>
<iframe id = 'ifrm' src = 'http://www.google.com' width = '700' height='500'></iframe><input type='button' onclick = 'fchange()' value = 'clickhere'>
</body>
</html>
From this I click the button and an alert box dispalys undefined. But I need the content or the source of the page ('http://www.google.com'). Please help me to do this.
Thanks in advance...
You can't do this, as it breaks the same-origin policy.
If both pages are on the same domain then you can with do what #Joel suggests, or the slightly more old fashioned:
window.frames['ifrm'].document.body.innerHTML;
You'll need <iframe name="ifrm" ...> for this to work.
If you want the source of the iframe, you would need to access the document object of the iframe.
function fchange()
{
alert(document.getElementById("ifrm").contentWindow.document.body.innerHTML);
}
As mentioned by others, you cannot get the source of an iframe which points to a page outside your domain.
You need to have back-end script for that. I think that's the only way. AJAX would not allow to make a request to other domains for security reasons.

Categories

Resources