0
$("#slideshow").click(function() {
    $("#content").fadeOut(function() {
        $(this).load('<?php $this->load->view("slideshow"); ?>', function() {
            $(this).fadeIn()
        });
    });
});

When I run the above code in Ci, CodeIgniter pends the 'slideshow' view to the beginning of the document. I need it to only display when the id of 'slideshow' is clicked. This code snippet usually works, but idk why it's acting funny now. Any ideas?

2
  • 1
    What the heck are you doing? What is in the slideshow view? Commented Jan 5, 2012 at 19:19
  • The first parameter of .load() is supposed to be a URL, not HTML or whatever you have in your view. Commented Jan 5, 2012 at 19:23

3 Answers 3

3

I'm not sure what you're trying to do here. View files in CodeIgniter are supposed to contain HTML. The Controllers can load the views, and pass them data if need be. Views are templates.

$(this).load('<div id="test"></div>', function(){});

This will throw an error, as .load expects a URL, not HTML (unless you're binding to the onload event, where the 1st param can be an array of data, but I don't think that's what you're doing).

What I assume you want is to have jQuery load the content of the view file into #content. To do this, you're gonna need a controller that outputs the view. For example:

Controller:

function slideshow(){
    $this->load->view('slideshow');
}

jQuery:

$("#slideshow").click(function() {
   $("#content").fadeOut(function() {
      $(this).load('/path/to/controller/slideshow', function() {
         $(this).fadeIn()
      });
   });
});
Sign up to request clarification or add additional context in comments.

1 Comment

@MichaelGrigsby: No prob, glad I could help :-)
1

This produces such a garbage that your browser will put it somewhere (at the beginning of the document):

$(this).load('<?php $this->load->view("slideshow"); ?>', function() {

Because the string needs proper encoding, e.g. as a javascript string with json_encode­Docs:

$(this).html(<?php echo json_encode($view_html); ?>);
$(this).fadeIn(); 

Check the CI docs how you get the view returned as string instead of being outputted. I leave this as an exercise.

3 Comments

@Rocket: An error? No idea, however I fixed the URL. Probably this needs a second json_encode?
$(this).load('<div id="test"></div>', function(){}); says Syntax error, unrecognized expression: >.
this: en.wikipedia.org/wiki/Data_URI_scheme#JavaScript - nevermind not all browser support that URI there, I replaced it with just html() which does that.
0

try do it:

$(this).load("<?php $this->load->view('slideshow','','true'); ?>", function() { ......  }

Note that i use single quote, and third parameter true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.