Link to home
Start Free TrialLog in
Avatar of mock5c
mock5c

asked on

Code needs to run after page is loaded

Below is a snippet of code that doesn't run when I load a page.  Only "-1-" is printed to the console.  It appears that the function inside is not loaded by the time the entire page has loaded.  However, in the firebug console after the page has loaded, I can manually run a commands such as:

$(".listings_display").find("a").removeAttr("onclick");
$(".listings_display").find("a").removeAttr("href");
$(".listings_display").find("a").css('cursor','not-allowed');

and it will remove onlick, href, and change the cursor.

I tried the delegate function and still the commands do not run when page is loaded.  It should do this when the user is not an admin so that the links are no longer active.

The 2nd part of this question is if there is a cleaner way for disabling, removing, hiding links instead of those commands I listed?  The block of code contains <br> for separating and I don't necessarily want to disable all links.  I want to display by a certain condition (such as if the string "ABC", "DEF", etc.  is 3 characters, then disable link.  But if it's 4 characters like "GHIJ" then do not disable the link.  I put in the line below with $(".listings_display").find("a").each(function(){ ...} but at this moment, I need to make sure this function runs when after the page is loaded.

if(!isadmin){
  console.log("-1-");
  //$("body").delegate(".listings_display", "load", function(){
  $(".listings_display").bind("load.listings", function(){
    console.log("-2-");
    $(".listings_display").find("a").removeAttr("onclick");
    $(".listings_display").find("a").removeAttr("href");
    $(".listings_display").find("a").css('cursor','not-allowed');
    //$(".listings_display").find("a").each(function(){
      //console.log("-3-");
    //});
  }).triggerHandler("load.listings");
}

Open in new window

<div class="row">
  <div class="grid2 first">
    <label for="listings">Listings</label>
  </div>
  <div class="grid8">
    <div class="listings_container">
      <input type="hidden" class="listings" id="listings" title="Listings" value="ABC DEF GHIJ KLM" tabindex="-1">
      <div class="listings_display">
ABC <a href="#" onclick="listingsManager.del('ABC', this); return false;" style="display: inline;">[Delete]</a> <a href="#" onclick="listingsManager.undo('ABC', this); return false;" style="display: inline;">[Undo]</a><br>
DEF <a href="#" onclick="listingsManager.del('DEF', this); return false;" style="display: inline;">[Delete]</a> <a href="#" onclick="listingsManager.undo('DEF', this); return false;" style="display: inline;">[Undo]</a><br>
GHIJ <a href="#" onclick="listingsManager.del('GHIJ', this); return false;" style="display: inline;">[Delete]</a> <a href="#" onclick="listingsManager.undo('GHIJ', this); return false;" style="display: inline;">[Undo]</a><br>
KLM <a href="#" onclick="listingsManager.del('KLM', this); return false;" style="display: inline;">[Remove]</a> <a href="#" onclick="listingsManager.undo('KLM', this); return false;" style="display: inline;">[Undo]</a><br>
      </div>
    </div>
  </div>
</div>

Open in new window

Avatar of Rob
Rob
Flag of Australia image

you need to wrap your code in the jQuery init function:

$(function() {
if(!isadmin){
  console.log("-1-");
  //$("body").delegate(".listings_display", "load", function(){
  $(".listings_display").bind("load.listings", function(){
    console.log("-2-");
    $(".listings_display").find("a").removeAttr("onclick");
    $(".listings_display").find("a").removeAttr("href");
    $(".listings_display").find("a").css('cursor','not-allowed');
    //$(".listings_display").find("a").each(function(){
      //console.log("-3-");
    //});
  }).triggerHandler("load.listings");
}
});

Open in new window

Avatar of mock5c
mock5c

ASKER

The code was already inside an init function $(function() { ... });
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial