1

i hae a link like ( domain.com/jsapi?key=123456 )

hov can i get this "key" into my JS code? i use jQuery and i don't know about its are easy way to get this "key" into JS variable.

tanks for all.

4 Answers 4

2

This plugin might helps: jquery url parser

key = $.url.setUrl($(yourlink).attr('href')).param('key');

(not tested)

Sign up to request clarification or add additional context in comments.

Comments

0

It's not jquery. It's pure javascript. You can use regexp.

str = "domain.com/jsapi?key=123456" # Take it from wherever you want
splitted = str.split(/\?key=([0-9]+)/)

Then you'll have an array in the "splitted" variable, it's second element (at the id 1) containing the value.

Comments

0

jQuery not needed. The query string is available from the DOM:

window.location.search.match(/key=([^&]*)/);

Which gives you an array that has your value in it.

Comments

0

You can use the URL constructor as follows:

let url = new URL('https://example.com/jsapi?key=123456');
console.log(url.searchParams.get('key')); // Outputs 123456

Using this method you can parse and get any part of a URL.

Important:

  • Note that I've added the protocol (https://) to the sample URL so I make sure it is a valid URL and it can be parsed.
  • Take into account the browser compatibility. You can check it here

For more details you can also the the specification.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.