I am able to get the querystring from url like this using regex and javascript: But I need to get rid of these %22...these don't show up in IE, just in FF..How do I do that? Ineeed everything after k=..but without %22..
<script type="text/javascript">document.write('<div class="DynamicSearchTitle">
Showing All Results For ' +
location.href.match(/\&k\=(.+)/)[1]+ ' Matches </div>');
</script>
URL
http://mysite/sites/dev/contact-us/Pages/LocationSearchTestPage.aspx?s=bcs_locations&k=%22Hospital%22%20OR%20%22Office%22
The URL is broken so I can't take a look at the whole code, but I think what you're looking for is the decodeURI-function.
decodeURI("%22")
for example would return "
Unescapeing the url from your question:
decodeURI("&k=%22Hospital%22%20OR%20%22Office%22");
returns &k="Hospital" OR "Office"
You can get all the Query String component by simple JS function described here
Use it like this,
var uparts = getUrlParts(location.href);
var the_K = uparts["k"];
Related
I am using C# and I have a CMS that uses an open text field. I am doing a redirect manually appending a query string BUT on top of that, I have editors putting UTMs into the URL. I am trying to track the redirects/vanity URLs so we an see the success of them, but the editors are adding UTMs and when I am transferring the redirect, I prepend "?ref=" to their URL to the second URL.
I need to know how to replace any subsequent question marks in the query string.
The CMS is seeing the second question mark and automatically redirecting to the homepage, because I think it is trying to be smart with the URL and the second question mark is causing it to think the URL is invalid.
So the original URL I am getting looks something like this:
www.mysite.com/somepage?utm_source=foo&utm_medium=bar
BUT it then redirects so I can track the URL and it looks now like this
www.myothersite.com/this-other-page?ref=www.mysite.com/somepage?utm_source=foo&utm_medium=bar
So what I want to do is in the second URL is to replace the second question mark with an ampersand. How would I do only the second or subsequent ones without getting rid of the first one?
I am using Javascript to do the redirect in the view.
My code so far
#{
var currentPageUrl = HttpContext.Current.Request.Url.AbsoluteUri;
}
<script type="text/javascript">
setTimeout(function () {
window.location.href = '#Model.Content.GetPropertyValue("externalRedirectURL")?ref=#currentPageUrl';
}, 200); //will call the function after 2 secs
</script>
I found my answer now using HTML.Raw and Json.Ecode.
When I did generic .Replace in the actual Javascript built string, for some reason the .Replace("?", "&") was adding another ampersand to anything in the query string with an ampersand already in the query string. Not what I expected.
Anyways, here is my answer
<script>
var currentP = #Html.Raw(Json.Encode(#currentPageUrl.Replace("?","&")));
setTimeout(function () {
window.location.href = '#Model.Content.GetPropertyValue("externalRedirectURL")' + '?ref=' + currentP;
}, 200); //will call the function after 2 secs
</script>
I have defined the following entry in my html:
script type="text/javascript" src="/dir/path/js/myApp.js?org=XYZ"></script>
Now i want to read the value "XYZ" as an input in myApp.js. How can i do this?
You can use split or match or some other String methods to extract the part you want to get.
Example below (you can run the code):
// Assuming this is the url you get
var url = '/dir/path/js/myApp.js?org=XYZ';
// Split the url and get the second part
var query = url.split(/\?/)[1];
var value = query.split(/=/)[1];
console.log('Query: ' + query);
console.log('Intended value: ' + value);
In your example, you want to read from a <script></script> tag.
To do so, it is easier to first give the <script> an id:
<script id="example" src="/dir/path/js/myApp.js?org=XYZ"></script>
Then you get the src by:
document.getElementById('example').src
Then from here you can modify the code example above for your use.
PS
If you want to read from the URL instead, you can use window.location.href to read the URL as a string.
I have a link example this
Now I want to get the source of this page and extract the md5 hash value which is something like
<strong>MD5:</strong> 1b061e5530d2612135b8896482e68e3c</div>
<div>
I want to get the value 1b061e5530d2612135b8896482e68e3c from it.
I have made an GET request and got the source code in an variable like:
$.get(link).done(function(data){
alert(data);
});
This seems to be working fine but I have no Idea how to proceed further kindly help me .
I have Searched but not got any helpful result.
You could use good, old fashioned regexp (i.e., the second result of /MD5:<\/strong> (.*?)<\/div>/g).
var result = (/MD5:<\/strong> (.*?)<\/div>/g).exec([text])[1];
var regex = /MD5:<\/strong> (.*?)<\/div>/g;
var output=document.getElementById("output");
var test="<strong>MD5:</strong> 1b061e5530d2612135b8896482e68e3c</div>";
var matches = regex.exec(test);
output.innerHTML="MD5 is "+matches[1];
<div id="output"></div>
You can use xpath to get the text node containing the MD5 hash value:
$.get(link).done(function (data) {
var md5Node = document.evaluate('//*[#id="app_info"]/div[1]/div[2]/div[2]/div[1]/text()', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
var md5String = md5Node.textContent;
// ...
});
A better XPath
It would be better to find the MD5 text node based on its sibling's value. The XPath would look like
//*[text()="MD5:"]/../text()
Does the div you're trying to get the value from have an ID? If so you could try something like this
$.get(link).done(function(data){ console.log($(data).find("#idofdiv").text()); });
You could also use jQuery's .load function, like this:
$('div#container').load('external-page.html div#md5');
What I'm trying to do is fetch a single piece of a string without using the hashtag element in the url. I already have a functioning code but it needs altering. So, how do I fetch any part of the url after ?.
Say I have ?fx=shipment+toys/fish-fix-fx/ as my url string; I want the button to show if shipment or fish or fx was my choice of selections for example.
Buttons showing with hastag: http://jsfiddle.net/66kCf/2/show/#iphone
Original JSFiddle (buttons not showing): http://jsfiddle.net/66kCf/2/
I want the iPhone buttons to show if fix was my choice: http://jsfiddle.net/66kCf/2/show/?fx=shipment+toys/fish-fix-fx/
try doing it with .split() and.match() like this...
var keys = window.location.href.split('?');
if (keys[1].match(/(fix|fish|fx)/))
{
$("#linkdiv").append(nextLink);
$("#linkdiv1").append(nextLink);
$("#linkdiv2").append(nextLink);
}
demo button showing : http://jsfiddle.net/LbKmf/show/?fx=shipment+toys/fish-fix-fx/
demo button not showing: http://jsfiddle.net/LbKmf/show/?reigel
Is this what your looking for:
"?fx=shipment+toys/fish-fix-fx/".split(/[\?=+\/-]/g);
window.location.search and split into array for comparisons
explained in How can I get a specific parameter from location.search?
http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/
Generally, Javascript doesn't have a built-in functionality for query string parameters. You can use string manipulation on window.location.search to get your parameters out of the URL string. Note that location.search includes the ? character too.
Something like this should do:
var queryString = function () {
// Anonymous function - executed immediately
// get rid of the '?' char
var str = "?fx=shipment+toys/fish-fix-fx/";
var query = str.substring(str.lastIndexOf('=')+1,str.indexOf('/'));
var vars = query.split("+");
for (var i=0;i<vars.length;i++){
console.log(vars[i]);
}
return vars;
} ();
I'm using javascript and would like to take a URL string that I have and break it down into its components such as the host, path, and query arguments.
I need to do this in order to get to one of the query arguments, which is itself a URL and is thus encoded in the original URL string.
I feel like there should be an easy way to do this in Javascript. Perhaps something that looks like this:
var what_I_Want = url("http://www.domain.com?queryArg1=somequeryargument").getQueryArgumentValue("queryArg1");
The parseUri function will do everything you need
Edit
Alternatively you can get the DOM to do the hard work for you and access properties on a newly created a object for different parts of the URL.
<script type="text/javascript" language="javascript">
newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
</script>
Hope this will help..
In javascript you can do this by using split() for the params and using the location object for the protocol and domain -- like Carl suggested
Also you can use parseUri as Tak suggested
There is also a jQuery plugin which makes parsing easier if you are already using jQuery in your project: https://github.com/allmarkedup/jQuery-URL-Parser#readme
Example:
$.url('http://allmarkedup.com?sky=blue&grass=green').param('sky'); // returns 'blue'
Probably not the greatest way of doing it but a simple method to get the query string in JavaScript would be to just use something along the lines of:
a = "http://www.domain.com?queryArg1=somequeryargument";
query = a.substring(a.indexOf('?')+1);
You could then split the query up based on the &'s and again on the = to get at whatever param you need.
Sorry if this ain't very helpful as its a bit of a low tech method :P
EDIT:
Just wrote a quick little JavaScript object to get URL Query parameters for you (sort of like) in your example. Only tested it in chrome but in theory it should work :)
//Quick and dirty query Getter object.
function urlQueryGetter(url){
//array to store params
var qParam = new Array();
//function to get param
this.getParam = function(x){
return qParam[x];
}
//parse url
query = url.substring(url.indexOf('?')+1);
query_items = query.split('&');
for(i=0; i<query_items.length;i++){
s = query_items[i].split('=');
qParam[s[0]] = s[1];
}
}
//Useage
var bla = new urlQueryGetter("http://www.domain.com?queryArg1=somequeryargument&test=cheese");
alert(bla.getParam('test'));