0

how do I replace the whole URL and then replace and load it with the new URL that is entered in a prompt by a user at the moment I have got this far

var website = prompt("enter a website");

document.location.href.replace = website;

I have tried different once but it adds the URL I already have. I want the whole URL replaced the url that needs to replaced

thanks

  • 3
    Possible duplicate of [How to redirect to another webpage?](https://stackoverflow.com/questions/503093/how-to-redirect-to-another-webpage) – IvanGrasp Feb 01 '18 at 20:26

3 Answers3

1

Try this:

var website = prompt("enter a website");
window.location.href = website;
Steve
  • 1,903
  • 5
  • 22
  • 30
  • I tried that it shows the URL but it doesn't replace the whole of URL as I m using a local file. – phpform10 asd Feb 01 '18 at 20:32
  • What exactly do you mean by "doesn't replace the whole of the URL"? Can you edit your question with an example of what is typed into the prompt and what the resulting URL is? – Steve Feb 01 '18 at 20:34
0

You can use window.location.replace() method.

Aaditya Thakkar
  • 1,750
  • 13
  • 13
0

Simple solution would be using

document.location.href

Example

document.location.href="https://flinkhub.com"

Remember to add http:// or https:// before website name for proper redirection otherwise it will add to the existing url.

Hope it solves your problem

Mohd Asim Suhail
  • 2,176
  • 1
  • 16
  • 23
  • document.location.href = "https/", website; <<< i have put this one but only add https and does not load the website – phpform10 asd Feb 01 '18 at 20:35
  • `var website = prompt("enter a website"); //enter like https://[website-name] window.location.href = website;` works for me 100%. – Mohd Asim Suhail Feb 01 '18 at 20:44
  • k i am using a local file and it does work but it doesn't redirect or load the page which is what i want it just adds to the local URL that I have got at the moment – phpform10 asd Feb 01 '18 at 20:47
  • i tested the code on local file also, it worked. Can you edit your answer with some example of what you are expecting. Some screenshot will also work – Mohd Asim Suhail Feb 01 '18 at 20:48
  • i have loaded a screenshot please take a look at it it's link on the question – phpform10 asd Feb 01 '18 at 20:58
  • I have recorded a video where you can see it's working. I hope that will solve your problem. https://filebin.ca/3qFKU49QBzKz – Mohd Asim Suhail Feb 01 '18 at 21:07
  • would it be possible if you can kindly paste the code as i m using public internet. – phpform10 asd Feb 01 '18 at 21:23
  • `
    Click me
    ` I created an html file with this code. Double clicked to open it in a browser then i clicked "Click me" to open prompt then i entered website name like "https://www.google.com".
    – Mohd Asim Suhail Feb 01 '18 at 21:25