JavaScript to redirect specific page

Hello,

I have a dropdown menu in our database, but unfortunately, when you click on the actual dropdown title, it leads to a blank page. There seems to be no way to edit that page, so I want to redirect it to the first page in the dropdown.

I'm really not good with JavaScript, but this is what I have so far:

 

$(document).on('knack-scene-render.scene_809', function(event, scene) {
if (window.location.hash.indexof("#resume-builder") != -1) {
window.location.replace("https://website.com");
}
else {}
});

 

Any ideas why the above doesn't work? I've tried a bunch of variations, but this seems to be as far as I can get. Thank you in advance!

In case anyone is interested, 380030416312 posted the correct code here:

https://support.knack.com/hc/en-us/community/posts/115000049411-Dropdown-Menu-Link

 

$(document).on('knack-scene-render.scene_284', function(event, scene) { 
window.location.href = "https://......";
});

Replace 'scene_284' with the scene number of your drop-down menu and add your redirect URL.

 

It worked beautifully for me. 

window.location.replace('http://example.com');

It’s better than using window.location.href = ‘http://example.com’;

Using replace() is better because it does not keep the originating page in the session history, meaning the user won’t get stuck in a never-ending back-button fiasco.

If you want to simulate someone clicking on a link, use window.location.href

If you want to simulate an HTTP redirect, use window.location.replace

You can use assign() and replace methods also to JavaScript redirect to other pages like the following:

location.assign("http://example.com");

The difference between replace() method and assign() method(), is that replace() removes the URL of the current document from the document history, means it is not possible to use the “back” button to navigate back to the original document. So Use the assign() method if you want to load a new document, andwant to give the option to navigate back to the original document.

2 Likes