Set query string parameter as input value [duplicate] - javascript

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

Related

indexOf in Javascript not working how it should [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I want to get the page number out if these possible strings:
$scope.projects.next can equal...
string 1) /api/project/?currentuser=false&page=2
string 2)
/api/project/?page=2
I have tried this:
$scope.projects.next.split('=')[1].indexOf("page")
However, this only works on string 2. How can I change this to make sure it finds the page number no matter what position or future arguments that could be added?
I have also tried:
$scope.projects.next.indexOf("page")
but this gives 52 no idea why.
You can use regex:
/page=([0-9]+)/.exec($scope.projects.next)[1]
That uses a regular expression with a capture group (the bit in ()) for the page number, and captures one or more digits after page=. The result is an array, where the first element is the whole match, and the second is the capture group (that's why we have [1] at the end).
please see here: http://jsbin.com/gizalo/1/edit
function to get parametr from string
function getParameterByName(name, string) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(string);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Controller
app.controller('firstCtrl', function($scope){
var stringa="/api/project/?currentuser=false&page=2";
var stringb="/api/project/?page=2";
$scope.a = getParameterByName('page', stringa);
$scope.b = getParameterByName('page', stringb);
});
The problem is that in the first url there are two = signs so your index would be out by 1 on the first url
$scope.projects.next.split('page=')[1]
This has absolutely nothing to do with AngularJS. But it's also simple, so why not answer. Splitting on a character splits on EVERY instance of that character. Your top string is going to end up being ['/api/project/?currentuser', 'false&page', '2'], so getting the position of [1]('page') isn't useful. Split it on ? instead.
var start = $scope.projects.next.indexOf("page=");
var end = $scope.projects.next.indexOf("&");
var page;
if(end == -1) {
page = $scope.projects.next.substring(start+5);
} else {
page = $scope.projects.next.substring(start+5, end);
}
This would also work for ?page=17&test=2

Split URL string to get each parameter value in javascript [duplicate]

This question already has answers here:
How to convert URL parameters to a JavaScript object? [duplicate]
(34 answers)
Closed 8 years ago.
var url = "journey?reference=123line=A&destination=China&operator=Belbo&departure=1043&vehicle=ARC"
How can I split the string above so that I get each parameter's value??
You could use the split function to extract the parameter pairs. First trim the stuff before and including the ?, then split the & and after that loop though that and split the =.
var url = "journey?reference=123line=A&destination=China&operator=Belbo&departure=1043&vehicle=ARC";
var queryparams = url.split('?')[1];
var params = queryparams.split('&');
var pair = null,
data = [];
params.forEach(function(d) {
pair = d.split('=');
data.push({key: pair[0], value: pair[1]});
});
See jsfiddle
Try this:
var myurl = "journey?reference=123&line=A&destination=China&operator=Belbo&departure=1043&vehicle=ARC";
var keyval = myurl.split('?')[1].split('&');
for(var x=0,y=keyval.length; x<y; x+=1)
console.log(keyval[x], keyval[x].split('=')[0], keyval[x].split('=')[1]);
to split line in JS u should use:
var location = location.href.split('&');

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 the parameter in html

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

Get image name out of image address with JavaScript [duplicate]

This question already has answers here:
How to pull the file name from a url using javascript/jquery?
(16 answers)
Closed 9 years ago.
I would like to get the image name out of the address.
This is the value I with the JavaScript:
http://localhost:51557/img/column-sortable.png
document.getElementById("ctl00_contentHolder_iSortColumn").value = columnNumber;
alert(imageName);
Whats the best way to get column-sortable.png out of the string?
As long as there's never anything after the image name in the URL (no query string or hash) then the following should work:
var str = "http://localhost:51557/img/column-sortable.png";
alert(str.substring(str.lastIndexOf('/') + 1));
Please refer to the split function:
var url = http://localhost:51557/img/column-sortable.png;
var elementArray = url.split('/');
var imageName = elementArray[elementArray.length - 1];
JSFiddle
If you want to try using Regex, check this out.
var imgURL = "http://localhost:51557/img/column-sortable.png";
var imageName = imgURL.replace( /^.*?([^/]+\..+?)$/, '$1' );
alert(imageName);

Categories

Resources