-1

JavaScrpt expert,

i want if the below script exist in my template coding then my page should redirect to example.com

   <script>
   $(document).ready(function(){
        $("#wrapper").hide();
   })
   </script>

if the above script exist in my template, then it should redirect to example.com

Attention: please add some condition in that script like this:

  <script>
  $(document).ready(function(){

  If   
  //#wrapper is hide

 $("#wrapper").hide();

  //then it should redirected to example.com

  </script>

I hope someone will figure out and will share code with me. thanks.

bradly
  • 25
  • 1
  • 8
  • Is there a reason you can't do the redirect immediately after the hide? – Taplar Jan 04 '16 at 23:58
  • @taplar do it immediately, if you can, i use need the script..i mean if inside the template this script exist $("#wrapper").hide(); then the page should be redirected to example.com..now how to make the script if wrapper is hide then page should redirected thats is. – bradly Jan 05 '16 at 00:04
  • http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-using-jquery#506004 Take a look at that. – Taplar Jan 05 '16 at 00:05
  • @Taplar i check that post, it is all about redirection, but i can't see any condition like i want...how to do that..? – bradly Jan 05 '16 at 00:24
  • It's not a conditional. You would just put that redirection logic after you hide. If what your after is to 'detect' when in any place in your code the element is hidden, that is more leaning towards some form of an Observer, which are not fully supported in all browsers yet. To confirm, is that what your after? To detect it being hidden? – Taplar Jan 05 '16 at 00:27

2 Answers2

2

If you need this functionality somewhere after the bit of code you show, this would work:

var $wrapper=$("#wrapper");
if($wrapper.length>0 && !$wrapper.is(':visible')){ 
    // #wrapper exists on the page but is not visible,  redirect user
    window.location.href = "http://example.com";
}
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • @DelightedDOD your script is the one what i want, its perfect for me....however i have one question....if i add both scripts in my template like this: first: and then var $wrapper=$("#wrapper"); if($wrapper.length>0 && !$wrapper.is(':visible')){ // #wrapper exists on the page but is not visible, redirect user window.location.href = "http://example.com"; } Then it work and my page is redirecting but – bradly Jan 05 '16 at 01:57
  • but if i re-arrange the script to use your's script before that one like this: var $wrapper=$("#wrapper"); if($wrapper.length>0 && !$wrapper.is(':visible')){ // #wrapper exists on the page but is not visible, redirect user window.location.href = "http://example.com"; } and then add this script then it is not working and my page is not redirecting, so why is not working if used that one before ? is there any way to work your script even if it is placed before or after. – bradly Jan 05 '16 at 01:59
  • @bradly If I had to guess, Id say because `$("#wrapper")` is visible on page load therefore if you hide it first with `$("#wrapper").hide()` `$wrapper.is(':visible')` in my code returns `false` and triggers my check but if you dont hide it `$wrapper.is(':visible')` in my code returns `true` and does not trigger my check. If this is the case, there is deffinatelyt a better way to achieve what you need, can you explain what you are actually trying to accomplish instead? WHat is the purpose if the cide, we can find a better method for you. – Wesley Smith Jan 05 '16 at 02:05
  • @DelightedDod i want whether my desired script used before or after in template it should work that is...it should not only work if it is placed after that script, in both cases your script should work thats i want. – bradly Jan 05 '16 at 02:22
  • @bradly What I mean is, there is no reason that you would explicitly set an element to hidden then check if it is visible (it will of course be hidden, you just hid it), there must be something more to your logic that is not known here – Wesley Smith Jan 05 '16 at 02:40
  • @bradly It would seem that you are using some sort of templating in your design, can you explain exactly how you're using the template and how this logic relates to that? – Wesley Smith Jan 05 '16 at 02:48
1

What Taplar says is:

   <script>
   $(document).ready(function(){
        // $("#wrapper").hide();
        window.location.href = "http://example.com";
   })
   </script>

If you need this behaviour in another place in your code, then see DelightedD0D answer.

Very good point by DelightedD0D, I've fixed the code. ;) DelightedD0D, I'd give you another point if I could.

tiomno
  • 2,178
  • 26
  • 31
  • @bradly I think this is what Taplar was saying, but, if this is what you need, you can dispense with the `$("#wrapper").hide();` since it would never be seen anyway :) – Wesley Smith Jan 05 '16 at 01:07