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");
});
Related
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";
I have this url http://localhost:1234/pag1
I want to be like below when I click on the div:
http://localhost:1234/pag1?fare=standar
and I have an onclick event function:
<div id="" onclick="addUrl();"> </div>
And the js code:
<script type="text/javascript">
function addUrl() {
var url = window.location.href;
window.location.hash = '?fare=standar';
};
</script>
This give me this result:
http://localhost:1234/pag1#?fare=standar
And I don't Know how to remove the (#). I only need to add the parameter to the url, without refresh the page.
Suggest me! Thank's.
Use window.history.pushState( {} , '', '?fare=standar' );
This should update the url without refresh.
You can also do a bit of reseach on pushState as the implementation can differ.
window.location.hash always put "#" in url
you can use to push paramter in url by using this function
function setGetParameter(paramName, paramValue)
{
var url = window.location.href;
if (url.indexOf(paramName + "=") >= 0)
{
var prefix = url.substring(0, url.indexOf(paramName));
var suffix = url.substring(url.indexOf(paramName));
suffix = suffix.substring(suffix.indexOf("=") + 1);
suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
url = prefix + paramName + "=" + paramValue + suffix;
}
else
{
if (url.indexOf("?") < 0)
url += "?" + paramName + "=" + paramValue;
else
url += "&" + paramName + "=" + paramValue;
}
window.location.href = url;
}
i hope this will helpful to you
Try this
HTML
<div onclick="addUrl()"> </div>
Javascript
<script type="text/javascript">
function addUrl() {
var url = window.history.pushState( {} , '', '?fare=standar' );
</script>
Quick Explanation : When user clicks on div, URL will update without refreshing the page.
Hope this will work for you,
thank you :-).
Try this
var myURL = document.location;
document.location = myURL + "?a=parameter";
Or
location.hash = 'a=parameter'
Or without reloading the page
window.history.pushState("object or string", "Title", "new url");
Or without reloading the page
window.history.replaceState(null, null, "/another-new-url");
Why dont you use jQuery? I prepared sample like here in JsFiddle:
$("#clickableDiv").click(function(){
var url = "http://localhost:1234/pag1";
var parameter = "fare=standar";
$("#AppendUrl").text(url+ "?" + parameter);
});
Is there a way to check the url params in jquery and append something to the url with refreshing the url.
So if current url path is:
http://someurl.com/test/
function myView() {
// if url does not have params of view='list' {
// update the url with http://someurl.com/test/&view=list
} else {
//dont update
}
}
var url= $(location).attr('href');
var val = url.indexOf('view');
if (val < 0) {
url += "?view=list";
window.location = url;
}
Check the article using Push and PopState:
http://www.hawkee.com/snippet/9940/
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">
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);