check the url params in jquery and append something to the url - javascript

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/

Related

Call one function after another to get the proper url

Hi I am working in grails ..
<g:link name="searchLink" controller="MRController"
action="ExcelExport">TEST GRAILS</g:link>
<script>
$(function() {
$('a[name="searchLink"]').bind('click', function() {
$(this).attr('href', $(this).attr('href') + '?startdate=' + start().StartDate +'&enddate=' + start().Enddate);
})
})
function start(){
var StartDate = $("#startDate").val();
var Enddate= $("#endDate").val();
return {StartDate:StartDate,Enddate:Enddate}
}
</script>
I have trying to get the following url
http://localhost:8077/Myproject/MRController/ExcelExport
?startdate=2017-05-21&enddate=2017-05-23
But when i click the link again i get the following url instead i want the link with new startdate and enddate value
http://localhost:8077/Myproject/MRController/ExcelExport?
startdate=2017-05-21&enddate=2017-05-23?startdate=2017-05-21&enddate=2017-05-23
Edited
I have tried with the following script to clear the parameters in the first call but i don't know how to make the script run one after another means first the url call work then the parameters will clear through the function
<script>
function refineUrl()
{
//get full url
var url = window.location.href;
//get url after/
var value = url.substring(url.lastIndexOf('/') + 1);
//get the part after before ?
value = value.split("?")[0];
return value;
}
</script>
Don't change the original href attribute in your JS-code:
$('a[name="searchLink"]').bind('click', function() {
document.location = $(this).attr('href') + '?startdate=' + start().StartDate +'&enddate=' + start().Enddate;
})

Edit URL before redirect

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");
});

how to change in below url redirect script

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">

javascript, if current url withour request part

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);

extracting src and setting as href value...after ?url= and before &addclient=

If my url is http://link-ads.blogspot.com/?url=http://stackoverflow.com/
The code below extracts http://stackoverflow.com/
<script type='text/javascript'>
$(document).ready(main);
function main()
{
$('#download, #imggg').attr('href', getIframeUrl() );
registerEvents();
resizeIframe();
}
function getIframeUrl()
{
var url = window.location.href;
var iframe_url = 'http://link-ads.blogspot.com/';
var param_start = url.indexOf("url=");
if( param_start != -1 )
iframe_url = url.substr(param_start+4,url.length-param_start-4);
if( iframe_url.indexOf("http://") == -1)
iframe_url = "http://" + iframe_url;
return iframe_url;
}
</script>
my problem is if the url is http://link-ads.blogspot.com/?url=http://stackoverflow.com/&adclient=12345678kkk1112 the code will extract the url as http://stackoverflow.com/&adclient=12345678kkk1112 when I want the url before &adclient= meaning I only want it to extract http://stackoverflow.com/
I'd suggest using a regular expression:
var url = "http://link-ads.blogspot.com/?url=http://stackoverflow.com/&adclient=12345678kkk1112"
url.match(/url=([^&]+)/)[1]

Categories