how to get the parameter in html - javascript

I create the html for Label & text box and OK button.
I just pass parameter from the javascript like that
../test/Html/MWD.Numeric.html?id=1.0000
I would like to set that id value in my text box #html.
How to set the id in text box. When I updated that id in html, I would like to update in parent screen.
How to do? Please kindly guide me. Thank in advance.

duplicate of How can I get query string values in JavaScript?
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); }

Related

Set query string parameter as input value [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
so with jQuery I'd like to take my url value example:
/finance-proposal?value=BD07UWT
And add it to this input element on my page:
<input name="field[21]" id="field21" class="input-text required-entry " style="" value="" type="text"></input>
I have tried to parse the url value however I have had no success.
Any idea what would be the most simple method of doing this with jQuery when the page loads?
Thanks, Nick
Use getParameterByName(How can I get query string values in JavaScript?) to get value from querystring provided below. You can assign value using .val()
var getParameterByName = function(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};
var value = getParameterByName('finance-proposal');
$('.input-text required-entry').val(value);

Javascript regex to get param value

I'm working on a code base someone else built and the search function uses this to get the parameter from the url to place as a value to search against. I've tried re-writing it, modifiying it, and everything else possible and can't figure out what is going on with the reutrn value from the function.
Here's the function
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
console.log(name);
var regex = new RegExp("[\\?&#;]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
console.log(results);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var part = getParameterByName('part_name');
The value of location.search is ?part_number=1234&go=GO
Then in the input box part_name if I type 1234 to be the value of the passed in parameter it will only show 12. However typing in other random numbers it will sometimes show them all correctly. If I type 124 as the search param then it displays correctly as searching against 124.
Is there something wrong with one of these regex statments that could be causing this, or is this something on the server side? The PHP seems fine on the backend but I'm not savvy on the Regex used in the Javascript code.
EDIT:
A testing snippet:
function getParameterByName(loc, name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
document.getElementById("res").innerHTML += "Name: " + name + " - ";
var regex = new RegExp("[\\?&#;]" + name + "=([^&#]*)"),
results = regex.exec(loc);
document.getElementById("res").innerHTML += results + "<br/>";
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var part = getParameterByName('?part_number=1234&go=GO', 'part_number');
var part2 = getParameterByName('?part_name=12&go=GO', 'part_name');
<div id="res"/>

Implement s2s pixel

I have to implement s2s pixel in website and my pixel is as
.
I have to implement it using javascript and they send me also javascript code as
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
</script>
In my landing page there is download button when a user clicks on that pixel should be fired plz help me in this

Match a string using a regular expression

I have the following string
name=cvbb&source=Mamma+Mia&startdate=2014-03-24
How can I match the value associated with name with regular expressions, namely the string "cvbb"
/[^name]=[^&]/ matches =cvbb but i want only cvbb
It looks like you want to get URL parameter value? In this case you could use this function :
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var value = getParameterByName('startdate');
That would put 2014-03-24 in a var named value
I am assuming you want to select the name out, right? For that you can use the expression:-
/name=([\w]+)&source=.*/
Explanation: The first word name is written right there. After that ([\w]+) will match a list of alphanumeric characters. Then & will come and stop our selection. If you want to check that the string starts with name then use
/^name=([\w]+)&source=.*/
Caution: Using [^name] means that the characters should not be n,a,m or e which is not what you want to check. I hope this helps.
try
var queryString = 'name=cvbb&source=Mamma+Mia&startdate=2014-03-24';
var theStringImAfter = queryString.match(/[?&]name=([^&]*)/i)[1]
Note that queryString can be a full url e.g. from window.location.href, you don't need to parse the query string first.
If you are passing an encoded string (which is the norm if you are including characters not supported in a url such as a space, ampersand or equal sign etc.) you will want to decode the string before you use it. This can be done with decodeURIComponent(theStringImAfter).
Here is the same approach wrapped up in a reusable function
function getArg(url, arg){
var v=url.match(new RegExp('[?&]' + arg + '=([^&]*)', 'i'));
return (v)?decodeURIComponent(v[1]):'';
}
Usage:
var theStringImAfter = getArg('name=cvbb&source=Mamma+Mia&startdate=2014-03-24', 'name');

How to get parameter like (parameter name contains) with javascript?

i am using the following method to get parameter by name:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
and i was wondering how to change it to get parameter that contains some string for example, i want to get the parameter that contains the word "document" but the whole parameter name is not "document".
please advise, thanks.
Like this?
val = location.search.match(/document.*?=([^&]*)/)[1]
Although I'd rather use a function that converts the whole query string into an object (like this one) and simply apply a filter afterwards:
params = urlParams()
ks = Object.keys(params).filter(function(k) { return k.indexOf("document") >= 0 })
if(ks)
value = params[ks[0]];
This way you can easily support multiple params (as in document1=foo&document2=bar) and avoid ugly dynamic regexp construction.
In older browsers you can use this instead of Object.keys/filter:
ks = []
for(var k in params)
if(k.indexOf("document") >= 0)
ks.push(k);
Suggestion:
function getParameterByPartialName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&][^&#]*" + name + "[^=]*=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
This however, will return only the 1st match, there might be more parameters that have a similar name. You could return an array of key-value pairs for all matches.

Categories

Resources