-2

This is my URL:

www.example.com/contact#test

Now I need to test. According to the research, I've found a site related to my problem, and I realized that I should use of hash for get test from my url. something like this:

var url = www.example.com/contact#test;
var anchor = url.hash;
alert(anchor);

but when I use of alert for anchor, it shows me undefined. anyway how can I get test from my url ?

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89
  • 1
    That says `Uncaught SyntaxError: Unexpected token ILLEGAL`, it doesn't show you `undefined`. Try creating a real test case. – Quentin Jul 20 '15 at 19:15
  • possible duplicate of [How do I get the value after hash (#) from a URL using jquery](http://stackoverflow.com/questions/11662693/how-do-i-get-the-value-after-hash-from-a-url-using-jquery) – Nicholas Thomson Jul 20 '15 at 19:16
  • @Quentin can I chat with you for a moment ? – Shafizadeh Jul 20 '15 at 20:02

3 Answers3

2

The most efficient way is using pure JavaScript

var hashValue = window.location.hash.substr(1);
Matt Busche
  • 14,216
  • 5
  • 36
  • 61
1

Easiest way to do is using split, if your URL has test only after the #. And also nothing other that test after the #.

var url = 'www.example.com/contact#test';
var anchor = url.split('#');
alert(anchor[1]);
Keerthi
  • 923
  • 6
  • 22
1
var url = new URL("http://www.example.com/contact#test");
console.log(url.hash.substring(1));
Andree Wille
  • 928
  • 1
  • 9
  • 22