-1

I'm having an issue where I'm trying to set a page's redirect destination to a URL from a JS function.

I've tried calling the function by using <meta http-equiv="refresh" in the header, but I either have the syntax wrong or <meta> simply doesn't allow for calling functions. I'm honestly not sure.

<head> 
 <script src="extFile.js"></script>
 <meta http-equiv="refresh" content="1; go2();" id="levP" name="levP">

 <title>SO Question</title>

</head>

go2() is a function from extFile.js which contains an if/then statement that provides different URLs depending on time of day. I'd like to have index.html redirect users via function go2() either by a method in the header or in the body.

If this should be handled in the body then I'd appreciate any feedback as to how that should look.

Jason B
  • 65
  • 6
  • Did you try `window.location.href = url` ? – XCS Aug 29 '19 at 20:22
  • Possible duplicate of [How do I redirect to another webpage?](https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage) – XCS Aug 29 '19 at 20:23
  • @Cristy I have. My issue is that `url` isn't static, it's a series of URLs which run depending on the time of day. There's an if/then statement which runs one URL, at a time, and it's in `function go2()`. Do you know if it's possible to set `window.location.href = function()`, or some variation of that idea? – Jason B Aug 30 '19 at 17:37
  • 1
    Your function can return the url. `window.location.href` API is just a variable assignment, you can set it to anything, including the return value of a function. – XCS Aug 30 '19 at 17:40
  • My issue is that there isn't one URL. I have more than 20 which are stored in `go2()`. I did try putting go2() in an external .js file and using `window.location.href = extFile.js`. It did make the redirection but what loaded was extFile's raw coding instead of executing it. – Jason B Aug 30 '19 at 19:28

1 Answers1

0

Like this? this code will redirect your page after 5 seconds

<head> 
 <script src="extFile.js"></script>
<script> 
setTimeout(function (){
  window.location = "https://stackoverflow.com/questions/57717282/how-to-set-redirect-to-links-provided-by-js-function"; 
}, 5000); 
</script>

 <title>SO Question</title>

</head>

if you want a to call a function do this:

<head> 
 <script src="extFile.js"></script>
<script> 
    var check = function(){
    window.location = "https://stackoverflow.com/questions/57717282/how-to-set-redirect-to-links-provided-by-js-function";
    }
    check();
</script>

 <title>SO Question</title>

</head>
x5qubits
  • 60
  • 7
  • Is there a way to integrate this with go2()? I guess I was hoping there's a way to have window.location call `go2()`, with `go2()` providing its own links. – Jason B Aug 29 '19 at 22:07
  • 1
    call go2 before redirecting – x5qubits Aug 29 '19 at 22:14
  • Sorry, I'm not fluent with JS. Do I replace `var check = function()` with `var check = go2()`? I'm a bit lost on where I should call it from. – Jason B Aug 29 '19 at 22:23
  • 1
    You have to modify inside your go 2 function to redirect ... use window.location = "yoururl"; and to call go 2 when page loads just replace check(); with go(); .... i am sorry but without seeing go2 function cant help more – x5qubits Aug 29 '19 at 22:29
  • Hi @x5qubits - I misunderstood you at first, I ended up getting it to work after a bit of troubleshooting. Thank you! – Jason B Aug 30 '19 at 22:51