简体   繁体   中英

Run Javascript only on one page

First my javascript:

$(document).on("ready page:change", function() {

 my_paginate();
}

function mypaginate(){
if ($('body.searches').length) {
  if ($('.pagination').length) {
      ....
      ....
      url = $(' .pagination .next_page').attr('href'); // i must change this and it works... but dont understand why if i had if statement for body.searches.
      }
    }
}

And my view:

 <body class="<%= params[:controller] %>">

What i want is to run this my_paginate() only on one page with controller searches. I try like that but its still load on every page. I try to do endless scroll for will_paginate like in rails cast 114 - but only for one will_paginate object, not for everyone on site. What more should i do? How run this script only on one page? Right now its load only when i go on page with searches controller but then it works on every .paginate class .

Try this:

function mypaginate(){
   if ($('body.searches').length > 0) {
      if ($('.pagination').length > 0) {
        .........

You could also do this:

if($('body.searches .pagination').length > 0){
      ....

Edit: I read your question again. An alternative to what you want to do is to put this in your view:

<script type='text/javascript'> mypaginate(); </script>

When you put javascript code inside the assets folder, all of them are precompiled and stuffed into one archive, so it would be available to all of your views.
One way to bypass this behavior is to put this javascript code at the bottom of your view, so only a particular view would get access to the mypaginate function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM