0

I need help in this. For example I have 3 pages (Page1.aspx, Page2.aspx, Page3.aspx) and I also have a modal popup where the user enters a url. Now, I want to test if the url that was entered is part of the application that I have.

What I've done so far is to use the Uri.TryCreate(url, UriKind.Absolute, out uriResult) method but it doesn't seem to fit the requirement because it does not work when the user passes google.com, it treats it like a relative url and not an absolute url.

Below are some urls which I test on and its respective result:

1 Answer 1

0

I want to test if the url that was entered is part of the application that I have.

For this you can use the location of the window object available:

function TryCreate(url) {
  if (url == window.location.host) {
    return "relative";
  } else if (url.split('?')[0] == window.location.host && url.slice(url.indexOf('?')) == window.location.search) {
    return "relative";
  } else {
    return "absolute";
  }
}

document.querySelector('button').addEventListener('click', function(e) {
  document.querySelector('pre').textContent = document.querySelector('input[type="text"]').value + ":" + TryCreate(document.querySelector('input[type="text"]').value);
}, false);
<input type='text'>
<button>check url</button>
<pre></pre>


<sub>due to some restriction you should add this url "stacksnippets.net"</sub>

4
  • could you try it with Page1.aspx, Page2.aspx, Page3.aspx? because it doesn't seem to work despite my changes on this.
    – Musikero31
    Nov 30, 2015 at 8:52
  • due to some restriction you should add this url "stacksnippets.net" you can see this in the answer. And BTW what issue do you face in it.
    – Jai
    Nov 30, 2015 at 8:54
  • stacksnippets.net is the window.location in the snippet if you run it.
    – Jai
    Nov 30, 2015 at 8:54
  • Actually, the stacksnippets.net is just one page. How about the rest of the pages (Page1.aspx, Page2.aspx, Page3.aspx)?
    – Musikero31
    Nov 30, 2015 at 9:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.