I was wondering how I would be able to execute a command such as this in javascript. I just want to store the url as a string in a variable and redirect them to it when the time comes -
var linkz = "http://www.google.com/";
window.location.href= linkz;
Why doesn't this work?
Thanks,
If you're using links this way (as mentioned in a comment):
Google
Then the problem is that you're not passing a string to your checkCk() function.
Try this:
Google
The code you used for window.location.href should work.
At this point, I don't see why you'd use javascript if all you're doing is replacing the default behavior of the link.
I've just tried on my local machine, and it works:
<script>
window.onload = function(){
var google = "http://google.com";
window.location.href = google;
}
</script>
Redirecting to google...
Copy this to new file, call it for example redirect.html and open it in your browser.
Update:
<script>
var redirect = function(new_place) {
window.location.href = new_place;
}
</script>
<a href='javascript:redirect("http://google.com")'>Go to google</a>
Related
I am trying to use the base url of my php page inside javascript tags but I don't know javascript very well. I use baseUrl;?> in PHP but that doesn't work in javascript. Can anyone tell me what I can use to replace baseUrl;?> below?
<script>
window.onload = function () {
BobbieEDITOR.replace("plados", {
contentsCss: "<?php echo $this->baseUrl;?>data/css/bltry.css"
});
};
</script>
Try this, use Referenced pages (below) to fine tune it:
<script>
window.onload = function () {
var baseUrl = window.location.host;
BobbieEDITOR.replace("plados", baseUrl + '/data/css/bltry.css');
};
</script>
References:
https://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/
Get current URL in web browser
http://tech-blog.maddyzone.com/javascript/get-current-url-javascript-jquery
I am trying to execute a js action that automatically sends informations to the URL for future use with php. To do so, I added a "onload" event on the window object and modified the URL in the listener. This is what it looks like so far:
window.addEventListener("load", function() {
window.location = "?test=test";
}, false);
When I load the page, the URL changes, but this is repeated over and over until the browser crashes. I was just wondering if there was a way to only execute it once.
If you don't want to use AJAX, and don't want to write JS functions to parse query strings, why not a simple:
<?php if(!isset($_GET['test'])): ?>
<script>
window.addEventListener("load", function() {
window.location = "?test=test";
}, false);
</script>
<?php else: ?>
<!--No JS written on the page, so no redirect -->
<?php endif;?>
But you really should look into AJAX, try using some JS framework like jQuery to help you in the process: http://api.jquery.com/jquery.ajax/
Check to see if you're already ON that page, before redirecting.
Add a simple flag on the URL like so: window.location = "?test=test&second=true";
As suggested in How can I get query string values in JavaScript?
Write a function to check get the URL params in JS GetQueryStringParams()
Then as suggested by others pass a send param in while reload
window.location = "?test=test&second=true";
Use the function in JS to check if you have a URL query string second and if its not there then reload the page
if you want to pass variables do this:
<?php
if(!isset($_GET['page'])){ //this line check if the variable field is available
$a = "Blank";
}else{
$a = $_GET['page'];
}
?>
that is it.
You could just erase the onLoad method once it's run, like so:
<script>
function doLoadStuff()
{
doSomeStuff();
//Clear onLoad contents
body.onLoad = function() {};
}
</script>
<body onLoad="doLoadStuff();">MyContent</body>
I need to make A Dynamic HTML Page to Redirect to A URL Using JavaScript.As a beginner, i need your help...What i want to do is to redirect to a url through a html page.For example: suppose a page address is www.example.com/pages.html?http://yahoo.com/news so this pages.html page in this case will redirect a user to yahoo.com/news ....I know how to do this with PHP but i cant understand how i should do it with javascript and HTML . any idea? Thanks
This should do it:
function Redirect(){
var current = window.location.href;
var exclude = current.indexOf('?');
window.location = current.substr(exclude + 1);
}
setTimeout("Redirect()",5000);
You can use window.location.href="new-url.html"; to go to a URL with JavaScript.
To parse the URL, use var theURL=window.location.href.split("?")[1];
location.href = location.href.split("?")[1];
Right now I'm showing an href that redirects to a certain location, I want to redirect automatically to the same link but don't know how.
This is the link:
<script>
document.write(' Go ');
</script>
I tried with window.location but I'm messing somehow since I go literally to the name of the function.
How should I do it? Thanks
Setting window.location.href should redirect you to a new page, like this:
var url = geoip_city(); // url should be a valid url string
window.location.href = url;
You should use window.location.href to redirect. So assuming you have a function which returns an url:
function geoip_city() {
...
return 'http://example.com?foo=bar';
}
you could redirect like this:
window.location.href = geoip_city();
How can I redirect a user to home page?
Example: mywebsite.example/ddfdf/fdfdsf and I want to redirect to mywebsite.example
However I want to do it without typing the static name. How can I do this?
document.location.href="/";
document.location.href="/";
or
window.location.href = "/";
According to the W3C, they are the same. In reality, for cross browser safety, you should use window.location rather than document.location.
See: http://www.w3.org/TR/Window/#window-location
(Note: I copied the difference explanation above, from this question.)
window.location.href = "/";
This worked for me.
If you have multiple folders/directories, you can use this:
window.location.href = "/folder_name/";
Can you do this on the server, using Apache's mod_rewrite for example? If not, you can use the window.location.replace method to erase the current URL from the back/forward history (to not break the back button) and go to the root of the web site:
window.location.replace('/');
maybe
var re = /^https?:\/\/[^/]+/i;
window.location.href = re.exec(window.location.href)[0];
is what you're looking for?
window.location = '/';
Should usually do the trick, but it depends on your sites directories. This will work for your example
strRetMsg ="<script>window.location.href = '../Other/Home.htm';</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", strRetMsg,false);
Put this code in Page Load.
var url = location.href;
var newurl = url.replace('some-domain.example','another-domain.example';);
location.href=newurl;
See this answer
https://stackoverflow.com/a/42291014/3901511
var url = location.href;
var newurl = url.replace('some-domain.example','another-domain.example';);
location.href=newurl;