How do you integrate an html link into a javascript code?

I'm trying to make a mobile version of my site. I made the site using Geodesic Solution's, GeoClassified, and now I'm using Mobify (http://m.mobify.me)/ to make the mobile version. I want to use a javascript to detect if the user is logged in or not, and then display the correct links if they are or aren't. Here's what I tried:

[code]

<SCRIPT type="text/javascript">

if (!$logged_in){

<a href = "http://m.mysite.com/index.php?a=10&amp;c="> Login </a>

|

<a href = "http://m.mysite.com/register.php"> Register </a>

}

else{

<a href = "http://m.mysite.com/index.php?a=4"> My Account </a>

|

<a href = "http://m.mysite.com/index.php?a=17"> Log Out </a>

}

</script>

[/code]

Unfortunately I can't get the above script to work. Any help would be greatly appreciated.

Thanks.

Comments

  • Unless your passing the $logged_in variable from another javascript this will not work because $logged_in isn't a valid javascript variable.

  • (!$logged_in)

    if that is a PHP variable, not sure that works. PHP was meant to generate html and it would takes some study to see how to pass PHP as an arg to javascript.

    Javascript works as DOM, provided you give an element an id tag and the other thing Javascript can do is RegEx. So put those two together and maybe... (you might could glean a cookie also)

    <html>

    <head>

    <title>Link Example</title>

    </head>

    <body>

    <a id="link_id" href="http://www.caesarscoffee.com.au/003.htm%22%3EClick here to continue Shopping.</a>

    <script type="text/javascript">

    lnk=document.getElementById("link_id");

    if(/[\d]{3}\.htm/.test(lnk.href)) lnk.href="http://www.caesarscoffee.com.au/ordernow.htm%22;

    </script>

    </body>

    </html>

    Logons in PHP can be tricky as the best solutions I've seen are a combination of php session with a cookie backing it up. Head First PHP & MySql has a working example as a chapter.

  • no one disables JavaScript. particularly people who like flash prefer it because of the fact that's often the only thank you to discover if the flash plugin is put in or no longer, and intensely virtually all web pages use javascript in recent times. So, JavaScript is usually a secure selection. there are a number of frameworks and libraries you ought to use to truly strengthen an application (e.g. jQuery, Dojo). And there are some exceedingly cool compilers that take larger point languages and generate javascript (e.g. CoffeeScript, Cappuccino, the google java to javascript stuff...). you're able to truly, quite attempt it. maximum open-source strategies are not polished sufficient yet, and the academic curve could be steeper, in spite of the fact that that's going to pay off.

  • <html>

    <head>

    <title>HTML Page</title>

    </head>

    <body> 

    <script type="text/javascript">

    var logged_in=false; 

    if (!logged_in){ 

    document.write('<a href = "http://m.mysite.com/index.php?a=10&c=%22%3E Login </a>'); 

    document.write('<a href ="http://m.mysite.com/register.php%22%3E Register </a>'); 

    } else{

    document.write(' <a href = "http://m.mysite.com/index.php?a=4%22%3E My Account </a>\n<a href = "http://m.mysite.com/index.php?a=17%22%3E Log Out </a>');

    }

    </script> 

    </body>

    </html>

    --php version--

    <?php

    if (!$logged_in){

    echo '<a href = "http://m.mysite.com/index.php?a=10&c=%22%3E Login </a>';

    echo' <a href = "http://m.mysite.com/register.php%22%3E Register </a>';

    } else{

    echo '<a href = "http://m.mysite.com/index.php?a=4%22%3E My Account </a>';

    echo' <a href = "http://m.mysite.com/index.php?a=17%22%3E Log Out </a>';

    }

    ?>

  • write the tag in document.write then u can add the link in java script

    as

    document.write("<a href = "http://m.mysite.com/index.php?a=10&c=%22%3E Login </a>");

  • Ehm..

    You can get JavaScript FH for free from this link http://j.mp/XXMvEt

    Cheers ;)

  • visit: www.w3schools.com/

Sign In or Register to comment.