Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Easier way to apply JS to all pages


Acheron

Recommended Posts

If you have ever tried to add a JS that is required on all pages (i.e. if you want to apply a JS to your infoboxes), you know it's a pain in the a-- to do so. Here's just a quick example of what I used to include various javascripts in all pages of my catalog.

 

New file, let's call it header_js.php:

 

?>

<script language="javascript"><!--
function thisfunction() {
 if ....
}
//--></script>
<?php
?>

 

Replace the function with your function(s) and place this file in /catalog/includes/

 

 

Then in all files with header tags ...

 

FIND:

 

<link rel="stylesheet" type="text/css" href="stylesheet.css">

 

ADD BELOW:

<?php include(DIR_WS_INCLUDES . 'header_js.php'); ?>

 

 

Now obviously, if you only add a JS once ever, this is just as much work as doing it the old-fashioned way. If you're like me though, there will always be things you want to improve upon, change, etc. With the above method, any time you want to add a new JS, you only have to modify 1 file (header_js.php) and the code will be applied to all pages. :)

Link to comment
Share on other sites

  • 2 weeks later...

Not bad, but personally, I'd just put the following in includes/header.php, just above the opening HTML:

<script language="javascript" src="includes/myscripts.js"></script>

Only one file to modify and then just create/edit the myscripts.js (in the osC includes dir) to apply to all pages. Plus, if the script is really long, it saves on the page load ;)

** Please do not PM with personal support requests (even if offering "payment"). Thank you.

Link to comment
Share on other sites

  • 2 weeks later...
Not bad, but personally, I'd just put the following in includes/header.php, just above the opening HTML:

<script language="javascript" src="includes/myscripts.js"></script>

Only one file to modify and then just create/edit the myscripts.js (in the osC includes dir) to apply to all pages. Plus, if the script is really long, it saves on the page load ;)

header.php is called in the <body> of the HTML. JS must be included in the HTML header tags.

Link to comment
Share on other sites

  • 7 months later...

Is there a way to get dynamic links in a js file. I would like to put ask a question in a js file but it will not call in the page info. Code below. What is happening is google is crawling the items page and picking up the ask a question code below and in googles description it adds Ask A question then will go on to list the item name and description...

 

http://oldworldcharms.net/french-boulle-tantalus-p-220.html

 

<a href="ask_a_question.php?products_id=<?php echo $product_info['products_id']; ?>" ONMOUSEOVER="window.status='.:: Ask a question about this item ::.' ; return true" style="text-decoration: none">Click Here To Ask Question</a>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...