New in JS, How do I check if one of my querystring has a value?
Example: I have result.aspx?loc=3. How do I check this in javascript if my querystring loc has a value. I need to do this because I will use $('#target').css({'display':'none'}); to a certain div
try location.search
The search property returns the query
portion of a URL, including the
question mark (?).
and can be called like so:
<script type="text/javascript">
document.write(location.search);
</script>
http://www.w3schools.com/jsref/prop_loc_search.asp
Related
<script language="Javascript" src="js/configurationJS.js?cid=${buildnumber}" type="text/javascript"></script>
I Have a Script tag like above and i don't know the purpose of ? before cid and also what cid=${buildnumber} indicates.
Thanks in advance.
The parameters can be get or post.
The get ones are shown in the url in this format:
url?param1=value1¶m2=value2¶m3=value3
In your configurationJS.js url you are passing a buildnumber value to the cid parameter.
For get request type and for passing any data (eg. id) from one page to another page, we append query params in URL and to differentiate the URL path and query params we use "?" symbols.
"?" act as separator, it indicates end of URL base path and start of query params.
Generally, We are using querystring to get the detail from the url. I am using the url like:
"example.com?q=abc"
in php with javascript. I need to use the url like
"example.com/abc"
Is it possible to use url like this "example.com/abc" ?
By definition, the QueryString is what is after the first question mark, see https://en.wikipedia.org/wiki/Query_string for example.
So your second url does not contain a QueryString. But it does not mean you cannot get the "abc" value with JavaScript: you can get the full url via
window.location.href
and then you can parse it.
I'm hoping to call a line of text by using a simple URL parameter. Say I had an ordered list in javascript and on load of url example.com/?i=14 would get the 14th line in my list and place it where desired.
How can I achieve this?
I'm not sure what you mean by "call a line of text," but maybe you could do this:
var url = window.location.href;
var queryPos = url.indexOf('i=');
var param = url.substr(queryPos + 'i='.length);
Now param will contain the value of the parameter and you could use it to fetch whatever.
But since you're trying to access a value from a URL with JavaScript, it might be better to make use of # as explained here: How do I get the value after hash (#) from a URL using jquery (there are non-jquery answers as well)
Hopefully this is what you need.
To place an array element where you need it on document load
<div id="placeHere"></div>
In JS
document.body.onload = function(){
document.getElementById('placeHere').innerHTML = array[14];
}
jQuery
$(document).ready(function(){
$('#placeHere').html(array[14]);
})
Let's say I have the following url string:
www.mysite.com?this&that&theOtherThing
I know how to add query strings to any link, but am unaware of how to remove one.
For instance, I know I can add a query string by doing the following:
//just add within the href
submit
or
//set the href within jquery
$('button').attr('href', currentUrl + '&that');
How would I remove that same query on click?
I tried the following, but got an NaN error:
$('button').attr('href', currentUrl - '&that');
Does anyone have advice on how to remove a query?
First, you want to replace "?that", not "&that". Second, you have an a tag that doesn't have an id, but you're setting the 'href' attribute of an element with the id of 'button', which doesn't appear in your code snippet.
You got a NaN error because you're trying to subtract "?that" from a string. Instead, use string.replace("?that", "") - which will remove the "%that", but not anything after it (if there is something). If there is something after "&that" in your string, get the indexOf("?that") and then set the string to a substring of itself like so:
html:
Click me
Javascript:
var urlString = $("#linktoModify").prop("href");
urlString = urlString.toString().substring(0, urlString.indexOf("?"));
$("#linkToModify").prop("href", urlString);
Also, keep in mind that your question is a bit misleading. You're trying to edit a string with javascript, not remove a query string. Query strings are readonly and cannot be removed, but you can easily modify a string, which appears to be what you're trying to do.
function remove_args(a, url, arg){
a.attr('href', url.replace(arg,''));
}
html
URL
try something like this. just a small scenario
I know just enough JS to get in trouble so please bear with me :)
I am using the WP-Properties WordPress plugin. When searching for properties it gives all results in a common search results page. I need to theme the search results page based on part of the search string so I need a body id.
Example of a search result url:
http://website.com/property/?wpp_search[pagination]=off&wpp_search[property_type]=oasis_park&wpp_search[lot_location]=Oceano+Lot&wpp_search[availability]=-1&wpp_search[phase]=-1&wpp_search[price][min]=-1
The part I want is what comes after: "wpp_search[property_type]"
In the above case it would be "oasis_park"
And this would then create a body tag of: <body id="oasis_park">
I tried to tweak the following code to get the specific part then have it write to the body tag but I can't get it to work in my situation: remove a part of a URL argument string in php
This will only work for this specific url, as you have not provided a general pattern for each url from which you will need to extract a substring:
var myString = "http://website.com/property/?wpp_search[pagination]=off&wpp_search[property_type]=oasis_park&wpp_search[lot_location]=Oceano+Lot&wpp_search[availability]=-1&wpp_search[phase]=-1&wpp_search[price][min]=-1";
var myVar = myString.slice(myString.indexOf("&") + 27, myString.indexOf("k"));
After you have identified a general pattern in every url you wish to check, you will then have to use a combination of substr(), slice() and indexOf() to get the substring you want.
Then, try
document.body.id = myVar;
or assign an id to body (e.g. "myID") then try this:
document.getElementById('myID').id = myVar;