Welcome Guest, Not a member yet? Register   Sign In
Adding JavaScript to head in 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.


Messages In This Thread
Adding JavaScript to head in view - by El Forum - 11-24-2009, 08:38 AM
Adding JavaScript to head in view - by El Forum - 11-24-2009, 10:16 AM
Adding JavaScript to head in view - by El Forum - 11-24-2009, 11:25 AM
Adding JavaScript to head in view - by El Forum - 11-24-2009, 12:06 PM



Theme © iAndrew 2016 - Forum software by © MyBB