• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Javascript Helper File Messing Up CSS Formatting???

I have put my javascript functions in helper files and then load them in the Controllers where needed.
When I do this though it messes up the formatting on my pages.
If I place the javascript directly inside the <head> tags though does not cause this problem.

I checked the html output in both cases and it looks fine from what I can see.
Any idea why that is?

What is the best way to store your javascript functions?

Javascript is supposed to be loaded/used in the <head>.

I store my javascript in javascript files, and then load them in the head of the document.

<skript src="http://mysite.com/assets/js/some_file.js" type="text/javascript" charset="utf-8"></skript>

With skript spelled correctly (forum removes them)

Thanks CroNiX,

That got it working but now my functions are there to view on every page, even though they are only needed if the user goes to a certain page.
How can I prevent it from doing that?

I also have several more functions that I want to create to run when another certain page loads. If I do as you suggest, wouldn't it try to run those functions every time every page loads, or is there a way to have it only load on the specific page I want?

Thanks for the help!

I use a template library, which allows me to select which css/js/etc get loaded, and I set that in the controller only loading what that controller needs.

I use a template system also but more simple than your probably.
I just have header, menu, main_content, footer.

To do like you are talking, would I break the header into 2 sections?
Could you give me an example of how that would work?

You can pass variables to your header like any other view. Those variables can be css files, js files or whatever else.
$assets['css'] = array(

$this->load->view('header', $assets);

Then in the &lt;HEAD&gt; of your header view,
if (count($css))
  echo '&lt;style type="text/css"&gt;';
  foreach($css as $c)
    echo '@import url("' . $c . '");';
  echo '&lt;/style'&gt;;
Same with javascript...etc.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.