0

As of now, on my page, the following html is working fine to trigger the following javascript function :

<a href="javascript:redirectToOtherSection();">Link to other section</a>

What I'd like to do is to have the javascript function triggered on page load when a specific variable is added to the URL. (Instead of having it as a link on the page)

For example, if url is www.mysite.com, page loads normally. If URL is www.mysite.com/?variable=1 , the script is triggered and user is redirected.

(I know there are other ways to redirect a user to another page, but I need to use this specific script, since the destination is dynamic from one type of user to another.)

I have very basic javascript knowledge and can't make the code I found here work : Triggering Script VIA URL . What they're doing is triggering a fancybox through URL variable. I believe that it's the same thing I want to achieve here. I'm just not to sure how to modify the code to go from "fancybox" to "redirectToOtherSection()"

   var $j = jQuery.noConflict();
$j(document).ready(function() {
    var url = window.location.href;
    url = url.toLowerCase();
    if (url.indexOf('globe=1') != -1) {
        $j("a#fancy").fancybox({
            'padding': 0,
            'overlayShow': false // extra comma removed
        });
    }
}); // extra curly bracket removed
$j("a#fancy").fancybox({
    'padding': 0,
    'overlayShow': false // extra comma removed
});

They are using jquery, not sure if jquery is needed in my case? Any help is much appreciated! Thanks

3
  • Are you referencing jquery in your script before hand? What errors are you seeing? Commented Oct 30, 2013 at 17:28
  • not sure if jquery is needed in my case. So this is not your code? Commented Oct 30, 2013 at 17:28
  • No, I just copy pasted the code from the other solution I found here : stackoverflow.com/questions/7900525/triggering-script-via-url Sorry for the confusion, I too believe I don't need jquery... Commented Oct 30, 2013 at 17:40

1 Answer 1

0

based on what you've said you dont need jquery, try

var url = window.location.href;
url = url.toLowerCase();
if (url.indexOf('variable=1') != -1) {  // where variable 1 is wqhats in you url
    redirectToOtherSection();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks for the code. I've tried it and doesn't seem to work. In Chrome developer tool I get "Uncaught ReferenceError: RedirectToOtherSection is not defined " But the link on the page using the same script still works fine onclick : <a href="javascript:redirectToOtherSection();">Link to other section</a> Any idea? Thanks again
Placed the code lower in the page (after the actual redirectToOtherSection function) and it works fine! Thanks
No worries. Ordering of js is often a pain, just remember that everything is executed from top to bottom so if you call a function that's further down the page, the browser won't yet know about it.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.