I have a search function, and would like to display the search term in the search input.
My url is: search-1.html?keyword=XXXXXX
How do I get this, and display it in an input?
Thank you in advance.
Use this:
http://ajaxcssblog.com/jquery/url-read-get-variables/
Take luck!
Oh and then you can use the following to display its value in an input field:
$("#inputId").val($.url.param("keyword"));
If it is just one key=value in the url you can use simple regex like this:
var theValueYouWant = window.location.href.match(/keyword=(.+)/)[1]
And set the value of an input like this
$('input').val(theValueYouWant)
If you want to parse the GET string more thoroughly, this function should do it...
gets = {};
$.each(location.search.replace(/^\?/,'').split('&'),function(k,v){
bits = v.split('=');
gets[bits[0]] = bits[1];
});
Regex solution:
var S = window.location.search; // "?keyword=..." etc.
var T = S.match(/^\?(?:[^\b]*&+)?keyword=([^&]*)/);
if (T)
T = T[1]
else
T = "no keywords found"
If multiple values are given for "keyword" (e.x. ?keyword=XXX&keyword=YYY), the regex will only find the first of these values (e.x. XXX). This regex works even if there are other variables in the query string.
jQuery-less solution:
<script type="text/javascript">
var $_GET=[],pairs=location.href.toString().substring(location.href.toString().indexOf("?")+1).split("&");for(key in pairs){pos=pairs[key].indexOf("=");$_GET[pairs[key].substring(0,pos)]=decodeURIComponent(pairs[key].substring(pos+1).replace(/\+/g," "))};
// Now just access with $_GET
// example...
keyword = $_GET["keyword"];
</script>
Related
I have strings in the following format in Pentaho Spoon:
"0.31;0.45"
Now I want these to split in Javascript
var str = "0.31;0.45"
var res1 = str.split(";");
However, this returns an array where only the first element is filled as
res1[0] = "0.310.45"
While I'm expecting this:
res1[0] = "0.31"
res1[1] = "0.45"
What am I doing wrong? I've been looking for quite some time now but didn't discover any solution.
The string has been sanitized before, because it was imported from an excel sheet where enters were added between the different values:
str = strold.replace(new RegExp("\n", "gi"), ";");
Snippet of the excel sheet data can be found here (can't disclose complete records due to the sensitive nature of the data):
Exceldata
Any pointers on the right track will be greatly appreciated :)
Thanks!
Rob
If you want to do this in a Modified JavaScript step, this should word fine:
After Excel-Input of the field test1 with value "0.31;0.45" (as string) your JS-Code should look like this:
var str = test1;
var split = str.split(";");
var res1 = split[0];
var res2 = split[1];
The new fields res1 and res2 now show the splitted values. I think your problem was a missing second variable here like split in my code above, maybe a kettle-specific thing...
As a second possible way to solve this without a JavaScript-Step use the Split-Fields-step:
After Excel input set the step Split-Fields. Here set the field to split (in my testscenario test1), set the delimiter (;) and define the two new fields.
Hope it works!
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 have an URL like the followin,
http://test.com/testing/test/12345
12345 is the id. I want to take this using query string. How to take this value in javascript?
try like this
http://test.com/testing/test/12345
var aarr = window.location.href.split('/');
//get last value
var id = aarr[aarr.length -1];
or just
var id = window.location.href.split('/').pop()
Use this :
document.location.href.split('/').pop()
Running it on this page yields : 22139563#22139563
Use this code:
var id = location.split('/').pop();
That's part of the path, not the query string... but you can access the page's URL using window.location.
The path is available at window.location.pathname which can be split up using forward slashes: window.location.pathname.split('/')
And then you can get the last item of the array: window.location.pathname.split('/').pop()
I would use substring, I think it's lighter than creating an array:
var id = window.location.href;
id = id.substring(id.lastIndexOf('/')+1);
I have this RegExp: var myReg = RegExp("https?://www.youtube.com/watch\?v=[^\"]+",""); to find the youtube link within a string. I then want to make the part of the string matching the RegExp a variable; lets say var url = "part of string matching the RegExp"
then I coudl do something like window.location = url; to redirect the browser directly to the video page. Thanks
You only have to access the first element of the result, if any:
var r = string.match(myReg);
if(r) var url = r[0];
Take care because is you dont find the url, the result will be a null value
This should do the trick:
myReg.exec(str);
var url = RegExp.lastMatch;
Udi
Okay, so I have some variables in javascript...
var link = 'http://8wayrun.com/streams/multi?type=3&pos1=1.teamsp00ky.video&pos2=1.teamsp00ky.chat&pos3=1.nycfurby.chat';
var position = 2;
As you can see, I have a link and a position. Using the position var I would like to replace some text in the link field. I would like to strip &pos2=1.teamsp00ky.chat from the link. Naturally, I have to do some basic regular expressions; the problem comes into when I try to use the position var in the regex. I just can't figure it out.
In PHP I could do the following:
preg_replace('/&pos'.$position.'=[^&]*/i', '', $link);
I tried the following in JS, but its not working:
link.replace(new RegExp('&pos'+position+'=[^&]*'), '');
Could someone help me out and tell me what I'm doing wrong? Also, how would I make it case-insensitive?
You need to set the value, not just call the method:
link = link.replace(new RegExp('&pos'+position+'=[^&]*'), '');
To make it case insensitive, use this regex:
new RegExp('&pos'+position+'=[^&]*', "i")
Although it might make it easier if you split the string on the "?", then split up the key/value pairs by "&", and then split them by "=".
Could someone help me out and tell me what I'm doing wrong?
replace does not mutate the string, but returns a new one - you'd have to assign it somewhere.
Also, how would I make it case-insensitive?
Pass the i flag to the RegExp constructor.
link = link.replace(new RegExp('&pos'+position+'=[^&]*', 'i'), '');
<div id="result"></div>
var link = 'http://8wayrun.com/streams/multi?type=3&pos1=1.teamsp00ky.video&pos2=1.teamsp00ky.chat&pos3=1.nycfurby.chat';
var position = 2;
var start = link.indexOf("pos2");
var end = link.indexOf("&", start);
document.getElementById("result").textContent = link.slice(0, start) + link.slice(end + 1);
on jsfiddle