duangutang3940 2015-09-28 07:40
浏览 76
已采纳

如果重定向来自特定网址,则调用javascript函数

so this is my question - I have a redirect in my .htaccess file, which works perfectly. But now I need to call a js-function on the index page IF the redirect comes from /video. Is this possible? I have spend quite a long time on trying to figure this out - there is no code examples yet, so right now it is theoretical.
PS - I am using wordpress, so I've just added this to the general .htaccess - Redirect 301 /video / I would love to hear your inputs and various methods of achieving this.

EDIT So I will post the updated tried out methods here - so far we got this -

<?php $redirectedFrom = $_SERVER['HTTP_REFERER'];
if(isset($redirectedFrom)): ?> 
<script type="text/javascript"> 
    console.log("does the console work"+ "<?php echo $redirectedFrom ?>"); 
</script> <?php endif ?> 

This would work magically, if the redirect comes from a click link to the index (ie. menues etc.) but when I just write /video in the URL, it seems like the HTTP_REFERER is not being set, and herby not accessing the IF. So do any of you have a fix for setting the referer, or some other solution?

  • 写回答

1条回答 默认 最新

  • dongpi0658 2015-09-28 07:48
    关注

    You can check for the value of :

    $_SERVER['HTTP_REFERER'] 
    

    It should contain url of previous page so you can detect your page from PHP and print something into HTML and later check with JS for printed value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?