Extra tags on the end of a URL for different pages [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Hello I really want to learn how to do something like this. If you go to a page for example it says http://example.net/search.html?catagory=food&includelevelone=true. I do not have access to php so it can only be HTML and Javascript/jQuery. Thanks in advance!

The part of the URL that is from the questionmark onwards is called a query string.
Here is a pure JavaScript function to parse the query string to obtain particular values:
function querystring(key)
{
var filter;
var value;
key = key.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
filter = new RegExp('[\\?&]' + key + '=([^&#]*)');
value = filter.exec(window.location.search);
if(value == null)
{
return '';
}
else
{
return decodeURIComponent(value[1].replace(/\+/g, ' '));
}
}
You just pass in the query string key name you're interested in (as a string) and you get the value back (also as a string.) An example of how you could use the function:
alert('Category = ' + querystring('catagory'));

Everything behind the questionmark is a url parameter. every word left of an equal sign is the name of the parameter, everything right of an equal sign is the corresponding value. The name-value-pairs are divided by &-signs
Here are two pages i quickly googled that are about getting these parameters in JavaScript (wich is really not that hard):
http://code-tricks.com/get-url-parameters-using-javascript
http://ziemecki.net/content/javascript-parsing-url-parameters

Related

Is there a way that this is possible? I am trying to find a value of a var constructed by other variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is a little bit of my code. I took out the complex parts. It is all on the script tag.
var trueenemyscoutx = '_2'
var tryeenemyscouty = '2'
var type_22 = "none"
var move1;
enemymove = "type" + trueenemyscoutx + trueenemyscouty;
var move1 = (enemymove.valueOf()).valueOf()
In the post, enemymove is a string constructed by concatenating "type" and two other variables, each of which is also a string.
The resultant string contains no information about the variables used and so the general answer to the question is no, it is not possible using the approach taken.
Obvious alternatives include:
Keep move a string, but use a separator (e.g. colon, comma or space) between component substrings. This allows extracting an array of the components using move.split(separator).
Use an array or simple object to hold move information instead of a string in the first place.

Returning query fragments [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an url that looks like this:
https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en
Is it possible to return every dk parameter separately? (no matter if there will be 1 or 5 dk parameters) so i would get separately sports, groupcompanyinsider, local.
If its not possible maybe there is a way to return all of them in one string like dk=sports&dk=groupcompanyinsiderlocal&dk=local ?
You can use the built-in javascript class URLSearchParams for this.
You can then transform this into the string you want with string concatenation and a foreach.
const url = "https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en";
var params = new URLSearchParams(url);
var result = "";
// concatenate individual values of the 'dk' query parameter
params.getAll('dk').forEach(function (item) {
result += '&dk=' + item;
});
result = result.substr(1); // remove starting '&' from the result;
console.log(result);
The result should contain your desired string.

assign key correctly to value in javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to map a user defined key to the associated value and it is currently returning undefined for the final (var final). The goal is to collect a user provided input (key) and map it to the associated string from the key:value object and then load a URL using this value. The web page loads incorrectly in the end because the value is undefined. Here is the code:
document.getElementById("btn").onclick = function onLoad() {
var converts = {
'apples' : 'green',
'sky' : 'blue'
};
var myTextField = document.getElementById("myTextarea");
var itemName = myTextField.value;
var final = converts[itemName];
if (document.getElementById('rad1').checked) {
window.open("somewebsite" + final + "restofURL", "this is a new window");
}
}
The problem is that the textarea has a lot of white-spaces in it by default. If the user adds a space, that could cause problems so we need to trim the string:
convert[ itemName.trim() ];
This will remove all the bordering whitespace.

How can you count the number of occurrences of all of the words in a text area using Javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
If I had a textarea and a user pasted a paragraph into the textarea, can statistics be run on the input using JavaScript on the client side? I was thinking of using an associative array or hash map so array[word] -> # of occurrences, and iterate through word by word but I'm not sure how to do this using client side JavaScript.
I tried searching JavaScript word count but I only get results on counting the total number of words, which is not what I'm looking for. What I am looking for is more keeping a count of each specific word. Can this be done in Javascript or Jquery? If so, how should I go about doing this?
Here is an approach for you
// first get an array of words
var words = text.split(' ');
// use Array.prototype.reduce (for example) to
// create statistics
var statistics = words.reduce(function (stat, word) {
if (!stat[word]) stat[word] = 0;
stat[word]++;
return stat;
}, {});
UPDATE: A little example which handles punctuation and upper/lower case, too: http://jsfiddle.net/1pnLzv8h/1/
Something like
var arra = ['ab','pq','mn','ab','mn','ab']
function wordCount(arra,val)
{
var ob={};
var len=arra.length;
for(var k=0;k<len;k++)
{
if(ob.hasOwnProperty(arra[k]))
{
ob[arra[k]]++;
continue;
}
ob[arra[k]]=1;
}
return ob[val];
}
alert(wordCount(arra,'ab')); //output 3

Regex in JavaScript for first SUM/MIN/MAX/AVG found in string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
What I'm trying to do is the following:
SUM(Sales) + SUM(Revenue)
or MIN(Sales) + SUM(Revenue).
What I want is the first calculation. So for 1), the result "SUM" will be given, and for 2) the result "MIN" will be given.
I've tried this for if statements but it's either impossible, or incredibly difficult to do that way. Could anyone guide me on potentially a RegEx way of doing this?
What I tried in if statements:
function hasFormula(formulaToLower) {
// formulaToLower could equal "SUM(Sales) + SUM(Revenue)" etc
// could also equal "SUM(Sales) + MIN(Revenue)" - this will return MIN, but it return SUM.
if (formulaToLower.indexOf('sum') !== -1) {
return "SUM";
}
if (formulaToLower.indexOf('min') !== -1) {
return "MIN";
}
}
Obviously though, this will bring out MIN first, even if it's found second, and so on...
You can use a regexp that allows all the combinations you want. The matches will be returned in the correct order if you use the global modifier, or only the first one will be returned if you do not:
var matcher = /SUM|MIN|MAX|AVG/;
var str1 = 'SUM(Sales) + SUM(Revenue)';
var str2 = 'MIN(Sales) + SUM(Revenue)';
console.log(str1.match(matcher)[0]) // SUM
console.log(str2.match(matcher)[0]) // MIN
*The [0] part takes the first element in the array of results returned by match.

Categories

Resources