Welcome Guest, Not a member yet? Register   Sign In
Adding JavaScript to head in view
#1

[eluser]Bazze[/eluser]
Hello!

I've been using my companies framework and it has the possibility to add JavaScript to the head-tag of the page by doing like this in the view:
Code:
<?php $this->scriptStart(); ?>
//javascript here
<?php $this->scriptEnd(); ?>
//rest of the view
<div>
</div>
//etc. etc.
Is this possible to do in CodeIgniter? Or does CI even preprocess the view? (I guess that's how it has to work)

Cheers
#2

[eluser]jedd[/eluser]
Hi Bazze and welcome to the CI forums.

What does scriptStart() actually do? Could you show the rendered view of the partial you've shown here?

It looks like it just echos the string '&lt; script type="text/javascript"&gt;' - which doesn't seem to be much of a saving.

You could rattle up a helper function called script_start() that did the same, if you prefer to have that kind of code in your view.
#3

[eluser]Bazze[/eluser]
[quote author="jedd" date="1259101005"]Hi Bazze and welcome to the CI forums.

What does scriptStart() actually do? Could you show the rendered view of the partial you've shown here?

It looks like it just echos the string '[removed]' - which doesn't seem to be much of a saving.

You could rattle up a helper function called script_start() that did the same, if you prefer to have that kind of code in your view.[/quote]

Okay, like this:

default.phtml (default layout)
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
  &lt;title&gt;Example Page&lt;/title&gt;
  &lt;?php print $styles; //Prints out default styles and styles added in controller with $this->addStyle('path/to/css/file.css'); ?&gt;
  &lt;?php print $scripts; //Prints out default scripts and scripts added in controller with $this->addScript('path/to/css/file.css'); ?&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php print $content; //this is where the view gets inserted ?&gt;
&lt;/body&gt;
&lt;/html&gt;

view.phtml (before render)
Code:
&lt;?php $this->scriptStart(); ?&gt;
//Just some sample JS
$(document).ready( function() {
  $("a").click( function(e) {
    alert('You pressed a link!');
    e.preventDefault();
  });
});
&lt;?php $this->scriptEnd(); ?&gt;
<div class="exampleClass">
  <h1>Hello, this is a title</h1>
  <p>Some text here</p>
  <div id="exampleID">
    <a href="#"><img src="image.jpg" /></a>
  </div>
</div>

after render
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
  &lt;title&gt;Example Page&lt;/title&gt;
  &lt;link rel="stylesheet" href="http://codeigniter.com/themes/forum_themes/support/support.css" type="text/css" media="screen, projection" charset="utf-8" /&gt;
  [removed][removed] //jquery
  [removed] //this is a script start html tag (it turns into [removed])
  $(document).ready( function() {
    $("a").click( function(e) {
      alert('You pressed a link!');
        e.preventDefault();
    });
  });
  [removed] //script end
&lt;/head&gt;
&lt;body&gt;
<div class="exampleClass">
  <h1>Hello, this is a title</h1>
  <p>Some text here</p>
  <div id="exampleID">
    <a href="#"><img src="image.jpg" /></a>
  </div>
</div>
&lt;/body&gt;
&lt;/html&gt;

Is this possible to acheive with CI? I mean, in CI you just make a header and footer file which you include and not the other way around (you have a layout where the view gets processed and included) as I am used to.
#4

[eluser]jedd[/eluser]
Dang this forum's blasted script-remover.

I modified my message - dunno if you can be bothered to do the same to yours - space after the < and maybe spread spaces through the word script.

Anyhoo, yes, I think I understand what you're trying to do. Have a read of the user guide's section on views, specifically the bit about using the third parameter set to TRUE to return the rendered version of that view partial to be echo'd later in a 'one big view rendering page'. Also see my [url="/wiki/Header_and_Footer_and_Menu_on_every_page_-_jedd/"]ramble in the wiki[/url] - nominally on using MY_Controller, but touches on using sub-view generators. I reckon it'd be fairly easy to modify that to suit what you're trying to do.

I'm writing a system at the moment, actually, that uses some javascript goodness - but using jquery my required code is only about a dozen lines, so I've left that in the primary view file for the time. I may well shuffle that into a separate file later, particularly if I want to start modifying some of the settings in it (which, as it happens, I do) dynamically, via PHP variable substitutions, before sending it back to the browser.




Theme © iAndrew 2016 - Forum software by © MyBB