I want do something: if something type url address like: www.mydomain.com/search(without request part), I want do something with javascript, fill with the url like www.mydomain.com/search?search=car, but nothing happened in my code.
<script>
var curent_url = document.URL;
if(document.URL.indexOf("?") < 0){ //if(!document.URL.match(/\?/)){
//alert('ok');
document.URL = curent_url+'?search=car';
}
</script>
Instead of document.URL, check for an empty document.location.search:
if (document.location.search.length === 0) {
// empty query string... append your part to doc
document.location.href = document.location.href + "?search=car";
}
Examine the document.location object in your console to see what else it offers. There's usually no need to parse out document.URL or document.location.href directly.
console.dir(document.location);
Related
I want to pass file name as instance to url using any redirect page method .
<script>
function my function() {
var x = document.getElementById("demo");
location.replace("localhost:57168/Hoopweb_Viwer.aspx?scs=" +x);
}
</script>
You can do something like this which uses window.location.replace to create simulated redirect, as you did in your code. Notice that reserved characters in the URL, after the protocol and domain name, are URL encoded to avoid errors.
I added http to your URL, but you can change this to whatever protocol is appropriate:
<script>
window.addEventListener("DOMContentLoaded", function() {
var x = document.getElementById("demo").value;
x = encodeURI("http://localhost:57168/Hoopweb_Viwer.aspx?scs=" +x);
location.replace(x);
});
</script>
I use the code below but the page still refreshes.
I'm trying to change the url or the page without refreshing it.
//getting the url from browser
var uri = window.location.toString();
//checking whether the url has anything after '?'
if (uri.indexOf("?") > 0)
//then take the url before '?'
var clean_uri = uri.substring(0, uri.indexOf("?"));
//And push this to browser
window.history.replaceState({}, document.title, clean_uri);
history.replaceState should not refresh the page. If page is reloading then you have something else in the code, like, for example, form submission. preventDefault() method can help you cancel the event.
<form>
<button id="bt" type="submit">Submit</button>
</form>
//getting the url from browser
var uri = window.location.toString();
var bt = document.getElementById("bt");
//checking whether the url has anything after "?"
if (uri.indexOf("?") > 0) {
//then take the url before "?"
var clean_uri = uri.substring(0, uri.indexOf("?"));
bt.addEventListener("click", function (e) {
e.preventDefault(); // If you remove this line, the page will refresh and the form will be submitted.
history.replaceState({}, document.title, clean_uri);
});
}
I want to put in my HTML/PHP a script for redirection if the website name does not match my word. I tried to use this code, but it doesn't work.
WITH REGEX
if (window.location.hostname !== 'myword.org'){
window.top.location.href = 'http://redirecttomysite.org';
}
var website = window.location.hostname;
var internalLinkRegex = new RegExp('^((((http:\\/\\/|https:\\/\\/)(www\\.)?)?'
+ website
+ ')|(localhost:\\d{4})|(\\/.*))(\\/.*)?$', '');
WITHOUT REGEX
if (website !== 'myword.org'){
window.top.location.href = 'http://redirecttomysite.org/forum';
}
try
if (website.indexOf('myword.org') == -1){
window.top.location.href = 'http://redirecttomysite.org/forum';
}
if(window.location.href.indexOf("myword.org") == -1) {
window.location = 'http://redirecttomysite.org';
}
simply as below
if (!(/stackoverflow\.com/i.test(location.hostname))){
...
similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
Here I am stuck with a scenario where I want to edit href of anchor tag before redirect. My scenario is if href attribute of anchor tag do not contain QueryString then append previous page query string to href means, suppose my current page url is xyz?x=2&y=4
Current href
href="local/services"
After Edit
href="local/services?x=2&y=4"
I tried.
HTML:
test link
JavaScript
function appendString(obj) {
var url = obj.getAttribute('href');
if (url.indexOf('?') != -1)
obj.setAttribute('href') = url + window.location.search;
}
also tried
function appendString(obj) {
var url = obj.getAttribute('href');
if (url.indexOf('?') != -1)
window.location.href = url + window.location.search;
}
but no success.
Please suggest a solution.
Thanks.
The parameters for 'setAttribute' are incorrect.
element.setAttribute(name, value);
Courtesy MDN : Element.setAttribute
Jed Burke pointed to your mistake. So you should use something like this:
function appendString(obj) {
var url = obj.getAttribute('href');
if (url.indexOf('?') != -1) obj.setAttribute('href', url + window.location.search);
}
or even simpler:
function appendString(obj) {
if (obj.href.indexOf('?') != -1) obj.href += window.location.search;
}
You can use JQuery for this:
$('.myLink').click(function() {
$(this).attr("href", this.href + "?param=1");
});
I have a url redirect script that works very well, but i want some change in it.
when i redirect outgoing link then at the end of redirected url i got something like '#lcinside'
example
http://dl.dropbox.com/u/36139836/unknown.html?urlc=http://imgur.com/
goes to
http://imgur.com/#lcinside
but i want to put #lcinside before url start.
example
#lcinsidehttp://imgur.com
i want to put adf url redirect link instead of #lcinside.
how can i put #lcinside before url .
below is script.
please help me.
<script>
function gup(sName)
{
sName = sName.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var sRegEx = "[\\?&]" + sName + "=([^&#]*)";
var regEx = new RegExp(sRegEx);
var aResult = regEx.exec(window.location.href);
if(aResult == null)
{
return "";
}
else
{
return aResult[1];
}
}
if(gup("urlc") != "")
{
window.location = gup("urlc").replace("lcamp", "&") + "#lcinside";
}
</script>
If you are just trying to scrub the referrer, maybe you should use meta refresh
<meta http-equiv="refresh" content="1;url=http://imgur.com/#lcinside">