views:

74

answers:

5

Hey there!

I'm trying to get to learn som javascript/jQuery stuff, and I'm really struggling with this one. It's basically a question of structure – how to do things in the best possible way.

I've got this function that randomly picks out a list item from one of three unordered lists (where all list items have been hidden), and shows it. Depending on what page I'm currently on, I would like to select a list item from one list in particular. All my lists are "marked" with an id, to make them unique.

I could pass an argument to my randomize function with the list id of my choice, to make it only select an item from that particular list. However, this would mean that I would have to place inline scripts in the html (one script tag with custom function call for each page), and from what I've heard inline scripts tehnd to block page rendering. And that would be bad, becuase I care about performance. Another way could be to have lots of if/else clauses, such as "if body has class the_classname -> randomize from the list with with id the_id". That would however mean that the script would have to make lots of unnecessary queries on the DOM.

Don't know if there's a term for what I'm talking about. Perhaps something like "conditional function calls" or maybe "page based function calls".

How would you tackle such a problem? I know my CSS & HTML, but am quite new to javascripting. Just trying to do the right thing...

A: 

Add it from the server-side at run-time: Page.RegisterClientScriptBlock Method


Customize it with a property from the code-behind;

function myFunction() {
        var theParameter = '<%# parameterName>';
        // do something
    }
Brad
+1  A: 

One way would be to create a javascript file that you include in the header of all your pages. The javascript file will contain your function that takes a pageId, and returns a list item based on the page

function getListItem(pageId) {
    switch (pageId) {
       ...
    }
}

Assign an ID attribute to the BODY tag of each page, corresponding to the pageId in your javascript function:

<body id="home-page">

Then, on your page load, you can pass in the ID value to your function using jQuery:

<script type="text/javascript">
    $(function() {
        var listItem = getListItem($('body').attr["ID"]);
    } );
</script>

This would pass in "home-page" to your javascript function and return the list item determined by the logic inside your javascript function.

XSaint32
I settled for something along these lines. Superb feedback. Wasn't really expecting anything and was surprised to see three answer in the blink of an eye :) Will need to read up quite a bit on JS and keep posting to stackoverflow. Thanks!
Unless
A: 

To circumvent the possibility of your scripts holding up the browser it's common practice to place your scripts just before the end of your body tag.

You may even want to look into asynchronous script loading.

t3hb4tman
A: 

Friend, I don't understand exactly what you want, but I'll try to help:

the inline javascript will block your page if you put them alone in the middle of your code, but you can say to them to execute only when the page was loaded with this code:

window.onload = function (){
  //call your function with the desired parameter
}

So when your page loads, the function on window.onload will be executed.

madeinstefano
+1  A: 

The very best way to do this would be to examine the url (no DOM traversal involved, no additional external scripts, so it's fast):

var path = window.location.pathname;
if (path.indexOf('/a_unique_url') !== -1) {
    // do code for the page 'a_unique_url';
} else if (path.indexOf('/another_unique_url') !== -1) {
    // do code for the page 'another_unique_url';
} else {
    // do default action, because none of the above were found.
}

A little more wordy on your part, but faster if you have a lot of unique operations would be to use a switch statement and the full path, like this:

var path = window.location.pathname;
switch(path) {
    case '/full/path/after/domain.html':
        // do code for the page '/full/path/after/domain.html';
        break;
    case '/some/other/path.html':
        // do code for the page '/some/other/path.html';
        break;
    case '/yet/another/path.html':
        // do code for the page '/yet/another/path.html';
        break;
    default:
        // do default action, because none of the above were found.
        break;
}

Although, if you have access to server side scripting, there are more efficient methods for these kind of operations.

Stephen