Welcome Guest, Not a member yet? Register   Sign In
Javascript Helper File Messing Up CSS Formatting???
#1

[eluser]dhall[/eluser]
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?
#2

[eluser]CroNiX[/eluser]
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.

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

With skript spelled correctly (forum removes them)
#3

[eluser]dhall[/eluser]
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!
#4

[eluser]CroNiX[/eluser]
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.
#5

[eluser]dhall[/eluser]
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?
#6

[eluser]CroNiX[/eluser]
You can pass variables to your header like any other view. Those variables can be css files, js files or whatever else.
controller
Code:
$assets['css'] = array(
  'http://somesite.com/css/mycss.css',
  'http://someothersite.com/css/theircss.css'
);

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

Then in the &lt;HEAD&gt; of your header view,
Code:
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.




Theme © iAndrew 2016 - Forum software by © MyBB