Author Topic: javascript+rel accessing proper syntax.  (Read 693 times)

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
javascript+rel accessing proper syntax.
« on: February 15, 2010, 03:49:08 PM »
Ok, i'm trying to dry my code atm and i'm wonder if this is the proper way to access the rel value of the <a> attribute on the page for the if then loop.

if('a[rel="reg"]'){
//set thigns
}

Or should it be something longer and complex? It seems that 'a[rel="reg"]' works for a conditional type thing in jquery but i'm unsure if it'll work here. since this is going to be a function i'll call whenever someone clicks on of the various links on the site that can use a "lightbox" type element. And since there'll be a certain amount of overlap such as the Terms of Use Agreement, Privacy Policy, Rules etc. Will be on a series of pages so it makes sense to me atleast to put this all in a function and add it to my functions.js file since it shall be being reused a fair bit.  If the syntax for straight up javascript is different feel free to correct. I just want to know how it's doing it so that i can attempt to make sure that the function will be called and be used once more to refill the current container instead of just having it die on each page so to speak.

Offline bbgames

  • Level 16
  • *
  • Posts: 138
  • Reputation: +1/-0
    • View Profile
    • Building Browsergames
Re: javascript+rel accessing proper syntax.
« Reply #1 on: February 15, 2010, 05:14:18 PM »
I assume this is jQuery - in which case, the best way to check this(instead of selecting on it) would be to do it like so:
Code: [Select]
if($(item).attr('rel') == 'reg') {
 // code goes here.
}
..where 'item' is the item that you passed in to your function.

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: javascript+rel accessing proper syntax.
« Reply #2 on: February 15, 2010, 05:36:37 PM »
I assume this is jQuery - in which case, the best way to check this(instead of selecting on it) would be to do it like so:
Code: [Select]
if($(item).attr('rel') == 'reg') {
 // code goes here.
}
..where 'item' is the item that you passed in to your function.
i was going to do the load function but put it within a function which is called when someone clicks on a link. And i'd have it set the variables which'd be set by said if then and then passed into the load function and proccessed to attempt to dry my code.

Offline bbgames

  • Level 16
  • *
  • Posts: 138
  • Reputation: +1/-0
    • View Profile
    • Building Browsergames
Re: javascript+rel accessing proper syntax.
« Reply #3 on: February 15, 2010, 06:21:14 PM »
What are you trying to do, exactly? If I knew a little more about that, I could probably help you out more.

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: javascript+rel accessing proper syntax.
« Reply #4 on: February 16, 2010, 04:16:16 AM »
ok since you seem to be pretty well versed in the ways of jquery, i'm attempting to keep my functions going after the first call. Atm, i'm using a panel that's hidden until it's called upon from someone clicking a link. Then that link causes it to become visible, it goes over the text, then it displays a section of the target page inside of it. Well, for some odd reason jquery has no idea how to intercept said links from inside of this panel or if i insert the new page onto my current page with teh scripts still outside of it. I have a link inside of said panel to the privacy policy for example, and i wnated to it to just load, but apparently it's not getting intercepted by my code which is on the actual page still. Then on the second part, i was going to move part of the register page onto the main page as to not have to load an entire page again, just the registerstration form and put that instead of the paragraph describing the game. But, the main problem with this is, even though i have made sure that my <script> tage is outside of that paragraph which is getting replaced it's not interecepting the link clicking as it should. it works just fine on any one page, but when i attempt to make it do it once more after it's loading once, it just fails and dies. I don't understand this and it's confusing me. Someone said to use the live() function to attempt to make this bit of code stick with it.

Code: [Select]
<script>
$(document).ready(function () {
    $('a[rel="tos"]').click(function (e) {
        e.preventDefault();
        $('#reusepanel').load('main/tos.php #tos', function () {
            $('#reusepanel');
reveal1();
        });
    });
$('a[rel="login"]').click(function (e) {
        e.preventDefault();
        $('#reusepanel').load('main/login.php', function () {
            $('#reusepanel');
reveal1();
        });
    });
$('a[rel="privacy"]').click(function (e) {
        e.preventDefault();
        $('#reusepanel').load('main/privacypolicy.php #privacy', function () {
            $('#reusepanel');
reveal1();
        });
    });
$('a[rel="reg"]').click(function (e) {
        e.preventDefault();
        $('#main').load('main/register.php #main', function () {
            $('#main');
        });
    });
});
if ('a[rel="reg"]'){
result = "#main";
urllink = "main/register.php";
containerid = "#main";
}
if ('a[rel="priv"]'){
result = "#reusepanel";
urllink = "main/privacypolicy.php";
containerid = "#privacy";
}
if ('a[rel="login"]'){
}
 </script>

now then, iw as going to set all of those click interceptors as one function and put it into my functions.js file but at the moment, it's still not working even on the page after i load in something else. It's as if it's just refusing to work at all. The other odd thing, is that i'm unable to force a fragment to override the current form's data but that's just adding int a little bit of code and nothing big. As in taking a fragment called #main and attempting to force it to overide the current #main on the current page. But that just adds in another bit of code and nothing big.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal