Jump to content

[SOLVED] <noscript> to tackle event handlers


quiettech

Recommended Posts

Hello all,

 

I'm having trouble defining how I should complement event handlers in a situation where javascript is either disabled or not supported.

 

I have a simple event handler:

<a href='#' onclick="doPrice('<?php echo $_SESSION['currency'] ?>')">Change to <?php echo $_SESSION['currency'] == 'EUR' ? 'Pounds.' : 'Euros.'; ?></a>

 

All doPrice() does is set a cookie and alter the DOM to display prices in the correct currency.

 

I want server side scripting to do this for me in case javascript is not available. The problem is not how to do it with PHP. The problem is how to correctly present a noscript tag to replace the above. How do you replace event handlers in no script situations?

Link to comment
Share on other sites

Well, a NOSCRIPT tag would not prevent the above link from showing - it would just not work for users that don't have JS enabled. IMHO, the best process is to first build the page with no JavaScript. Then once it is complete find where you can add JS to make it more user-friendly.

 

In this case, though I think there is an easy solution. Add an actual link to the href property. I would suggest linking back to the current page, but adding a parameter on the URL for the currency to be displayed (You would need to modify the page to accept that parameter and display the appropriate currency on reload). Then that link will work for users that do not have JS enabled.

 

But, you don't want that link to refresh the page for users with JS enabled. So, just add a "return false;" to the onclick property and that will prevent the link from actually firing for JS users.

 

<?php
$changeCurrency = ($_SESSION['currency']=='EUR') ? 'Pounds.' : 'Euros.';
?>

<a href="<?php echo $_SERVER[php_SELF]?currency=$changeCurrency; ?>" 
   onclick="doPrice('<?php echo $_SESSION['currency'] ?>');return false;">
Change to <?php echo $changeCurrency; ?>
</a>

 

Link to comment
Share on other sites

Thanks mj,

 

Another thing that occurred to me after posting (it's always after posting, bleh!) is that I could document.write the whole anchor tag. On that case a <noscript> tag would become a viable alternative.

 

Anyways, either way it's solved. Thanks again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.