Passing Identifiers to Javascript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Passing Identifiers to Javascript

Post by Buddha443556 »

Been working on a project that handles users comments on HTML pages, a drop-in Javascript. I need to pass a unique identifier for each HTML page to the Javascript. I figure I can pass this ID the following ways ...
  1. Hard coded in-line Javascript

    Code: Select all

    <script type="text/javascript">
    var pid = 1; //Page ID
    </script>
    PROS: Simple. Standard.
    CONS: ?
  2. Via the script source URL

    Code: Select all

    <script type="text/javascript" src="/javascripts/nifty.js?pid=1"></script>
    PROS: ?
    CONS: Need to dynamically generate the Javascript. Caching?
  3. Via the URL for HTML

    Code: Select all

    http://www.domain.com/rant_about_programming_number_94.html?pid=1
    PROS: ?
    CONS: Passed via GET. Easily messed with by users. Screws up a pretty simple URL scheme.
  4. Via Meta Tags

    Code: Select all

    <META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=US-ASCII">
    <meta name="keywords" content="programming rant">
    <meta name="description" content="Just the rantings of a senile old programmer.">
    <meta name="author" content="Buddha">
    <meta name="published" content="Tue, 14 Nov 2006 16:20:12 +0000">
    <meta name="updated" content="Tue, 16 Nov 2006 18:36:18 +0000">
    <meta name="pageID" content="1">
    
    PROS: Sort of the purpose of Meta tags, indexing. No in-line Javascript just to set a variable.
    CONS: May effect SEO? Never seen Meta tags used this way.
In-line Javascript is the standard way to pass such values. However, I'm seriously considering using the meta tags. Any thoughts on using Meta tags this way?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I assume having javascript enabled will be a requirement for your users?

if so, I'm having a hard time convincing myself there is a better alternative to the first option you listed.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Burrito wrote:I assume having javascript enabled will be a requirement for your users?

if so, I'm having a hard time convincing myself there is a better alternative to the first option you listed.
I like the meta tags approach. It just "feels" more scalable.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

d11wtq wrote:It just "feels" more scalable.
in what way?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Burrito wrote:
d11wtq wrote:It just "feels" more scalable.
in what way?
I'm not sure. That's why I said "feels". Generally, anything that isn't directly set into the code itself is more flexible in the first place. It would be easy to write a function such as getMetaVar("author") and that's your scalability. All you need to do when you have new parameters is set them in meta tags. I know you could do the same thing by generating the relevant JS but it just doesn't feel as clean. I'm sure it's 100% personal preference though. It's kinda like the discussion of using an array for configuration, or using XML, or using a config object.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Burrito wrote:I assume having javascript enabled will be a requirement for your users?
I'll directed them else where to view or post comments if Javascript is disabled.
Burrito wrote:I'm having a hard time convincing myself there is a better alternative to the first option you listed.
Me too. That's why I asked. :D
d11wtq wrote: I'm sure it's 100% personal preference though.
Your probably right about that. I just can't think of a good reason not to try it. I doubt it would effect SEO which is my only real concern.

I'm thinking of using the Meta information for indexing the page so I need to process it anyway. Which is why it occurred to me I could put this one variable in a Meta tag instead of using the in-line Javascript. It's going to be the index.
Post Reply