PimenTech-scripts : jquery.jframe.js

Thanks to jQuery library, jFrame provides an easy way to get an HTML frame-like behaviour on DIV Elements with AJAX. It comes with Pimentech Scripts library. Also alvaiable on jQuery_site .

With jFrame, you can build smart, complex modern, internet apps without leaving the main page, without a single line of JavaScript !

Requirements

The following JavaScript files must be included in the head section of your HTML page :

Usage Example

Here a demo.

index.html

<?xml version="1.0" encoding="utf-8"?>
<html>
   <head>
      <script type="text/javascript" src="jquery.js"></script>
      <script type="text/javascript" src="jquery.form.js"></script>
      <script type="text/javascript" src="jquery.jframe.js"></script>
   </head>
   <body>

       <div id="jframe1" src="#">
           <a href="index.html" target="jframe2">
              on click, index.html will be loaded in "jframe2" div
           </a>

           <a href="test.html">
              Without target, test.html will be loaded in the first parent jFrame : jframe1
           </a>

           Form inside jFrame. Note the target attribute on submit button.
           <form method="post" action="test.html">
               <input name="toto" value="popo" />
               <input type="submit" name="submit" value="Search" target="jframe2" />
           </form>
       </div>

       <div id="jframe2" src="test.html">
            Second jFrame. This content will be replaced by "test.html"
       </div>

       <a href="index.html">Normal link outside jframe</a>
   </body>
</html>

A jFrame is a DIV tag with a src attribute. In a jFrame, click events on <a> and <input type="submit"> tags will be handled by jframe and loaded in ajax.

Elements

jFrame : DIV with src attribute

Attributes

  • id : jFrame id, required.
  • src : url or "#", required. After a jFrame load, the src attribute value is replaced by the loaded url.

Links and Submit Buttons

Inside jFrames, onclick events on each link and submit button are redirected to jFrame plugin.

A link

With target attribute, the href value is loaded by an xmlhttprequest in <div id=target>. Without target attribute, the href value is loaded in the current jframe. JFrame can be desactivated on a particular link with jframe="no" attribute (same for submit buttons).

INPUT type="submit"

The submit buttons also support a target attribute, like <A> tags.

jFrame forms support get and post method, onsubmit form attribute.

BUTTON type="submit" are also supported.

jQuery API

  • $(div element).loadJFrame(url, callback) : like ajax.load function.

  • $(div element).activateJFrames() : the div element becomes a jFrame.

  • $(element).getJFrameTarget() : if exists, return the first jFrame parent element.

  • $(element).waitingJFrame() : Overload this function in your code if you want for example a "waiting" message. Example :

    jQuery.fn.waitingJFrame = function () {
       $(this).html("<b>loading...</b>");
    }
    

Source

// jFrame
// $Revision: 1.101 $
// Author: Frederic de Zorzi
// Contact: fredz@_nospam_pimentech.net
// Revision: $Revision: 1.101 $
// Date: $Date: 2008-04-29 11:36:24 $
// Copyright: 2007 PimenTech SARL
// Tags: ajax javascript pimentech english jquery


jQuery.fn.waitingJFrame = function () {
    // Overload this function in your code to place a waiting event 
    // message, like :  $(this).html("<b>loading...</b>");
}


jQuery.fn.onUnloadJFrame = function () {
    jQuery(this).find("div[src][onunload]").add(jQuery(this))
    .each(
          function(i) {
              if (eval(jQuery(this).attr("onunload")) == false) {
                  return false;
              }
          });
}

function jFrameSubmitInput(input) {
    var target = jQuery(input).getJFrameTarget();
    if (target.length) {
        var form = input.form;
        if (form.onsubmit && form.onsubmit() == false 
            || target.preloadJFrame() == false) {
            return false;
        }
        jQuery(form).ajaxSubmit({ 
            target: target,
                    beforeSubmit: function(formArray) { 
                    formArray.push({ name:"submit", value: jQuery(input).attr("value") }); 
                },
                    success: function() { 
                    target.attr("src", jQuery(form).attr("action"));
                    eval(target.attr("onload"));
                    target.activateJFrame(); 
                }
            });
        return false;
    }
    return true;
}

jQuery.fn.preloadJFrame = function () {
    if (jQuery(this).onUnloadJFrame() == false) {
        return false;
    }
    jQuery(this).waitingJFrame();
}


jQuery.fn.getJFrameTarget = function () {
    // Returns first parent jframe element, if exists
    var div = jQuery(this).parents("div[@src]").get(0);
    if (div) {
        var target = jQuery(this).attr("target");
        if (target) {
            return jQuery("#" + target);
        }
    }
    return jQuery(div);
};


jQuery.fn.loadJFrame = function(url, callback) {
    // like ajax.load, for jFrame. the onload attribute is supported
    var this_callback = jQuery(this).attr("onload");
    callback = callback || function(){};
    url = url || jQuery(this).attr("src");
    if (url && url != "#") {
        if (jQuery(this).preloadJFrame() == false) {
            return false;
        }
        jQuery(this).load(url, 
                     function() { 
                         jQuery(this).attr("src", url);
                         jQuery(this).activateJFrame(); 
                         jQuery(this).find("div[@src]").each(function(i) {
                                 jQuery(this).loadJFrame();
                             } );
                         eval(this_callback);
                         callback();
                     });
    }
    else {
        jQuery(this).activateJFrame(); 
    }
};

jQuery.fn.activateJFrame = function() {
    // Add an onclick event on all <a> and <input type="submit"> tags
    jQuery(this).find("a")
    .not("[jframe='no']")
    .unbind("click")
    .click(function() { 
            var target = jQuery(this).getJFrameTarget();
            if (target.length) {
                var href = jQuery(this).attr("href");
                if (href && href.indexOf('javascript:') != 0) {
                    target.loadJFrame(href);
                    return false;
                }
            }
            return true;
        } );

    jQuery(":image,:submit,:button", this)
    .not("[jframe='no']")
    .unbind("click")
    .click(function() { return jFrameSubmitInput(this); } );

	// Only for IE6 : enter key invokes submit event
    jQuery(this).find("form")
    .unbind("submit")
    .submit(function() {
			return jFrameSubmitInput(jQuery(":image,:submit,:button", this).get(0));
    } ); 
};


jQuery(document).ready(function() { 
    jQuery(document).find("div[@src]").each(function(i) {
            jQuery(this).loadJFrame();
    } );
} );

Commentaires

Mike Septembre 23, 2007 at 12:39 après-midi

Hi.
This is really very useful. Thanks a lot !

I just saw one problem :

To select submits, you use
$(this).find("input[@type='submit']")
but submit can also be buttons with type =submit.
(thats what is use with struts2 ....)
So you should put instead
$(this).find("input[@type='submit'], button[@type='submit']")
I tested and it works.

fredz Septembre 24, 2007 at 5:18 après-midi

Done ! Thanks for your support.

milton Septembre 26, 2007 at 7:48 matin

Has anyone got a functioning demo of this in action?

Serge Septembre 28, 2007 at 11:40 matin

If you select a person in the demo, then click edit, save it, and THEN click edit again, the edit part is loaded.

Stan Septembre 28, 2007 at 4:20 après-midi

Fixed.
The people_list div is now reloaded when a people_detail form is submited.

Javascript links Octobre 5, 2007 at 6:02 matin

Are you planning to support javascript links using "this"

fredz Octobre 5, 2007 at 9:15 matin

? Javascript links are ignored by jFrame

edwo Octobre 6, 2007 at 8:58 après-midi

XHTML do not support 'src' tag, any suggestions?

daniel Octobre 15, 2007 at 12:32 après-midi

I like it, but same problem, src tag ist not xhtml valid - any suggestions?

fredz Octobre 15, 2007 at 12:49 après-midi

Write to the W3C ?
Seriously, what is the real problem ? jFrame lib is a kind of html extension, I prefer adding an explicit attibute like src, rather than reusing wrongly a valid attribute.
But if you have a better idea, we are open.

Peter Octobre 19, 2007 at 6:16 matin

Has anyone used the jQuery Taconite plugin with jframe using a form?

I designed a form within a <DIV> with SRC="#", with a TARGET to another DIV, that will return data formatted for Taconite (XML) .

The firebug plugin shows at the end: Node cannot be inserted at the specified point in the hierarchy.

However, if I do a regular <A> to the CGI that returns the same data, Taconite works fine and the desired effect is achieved.

Any suggestions? Seems to be a problem of a combination of forms (jform is being loaded), Taconite and jframe. Any suggestions?

Jon Novembre 13, 2007 at 1:27 matin

It appears to me that jFrame breaks the jquery validate plugin. I am not sure who's "fault" it is, but my guess is that because you are overriding the onclick behavior, the form submission never gets to happen.

I just finally figured out there was a conflict, so now I'll start looking to see how I can change the jFrame code to not conflict. My guess is that I remove the automatic "jframing" of the form, and manually call it from the validation code on form submit.

Jon Novembre 13, 2007 at 3:29 matin

Ah, got it.

If I add:
if(!($(form).validate().form())){
return false;
}

just below the if(form.onsubmit...) in jframe.js, everything works out fine.
I guess the right solution would be for jframe to support a callback. I think the problem has to do with the form.onsubmit that you are calling doesn't really do the right thing, if other ajax controls are in place. Something like that.

James! Novembre 19, 2007 at 5:19 matin

Excellent stuff - stop complaining people, it suis most peoples needs - quick and easy ajax - I love it!!
If you can'cope with the compliency write ajax yourself - it's not THAT difficult!!!

fredz Novembre 19, 2007 at 9:07 matin

To Jon :
You can do like that :
<form id="f" onsubmit="return $('#f'').validate().form()">

(beware, you can't write "this.validate().form()" cause the onsubmit attrivute is "evaluated" by jframe)

To James! :
Thanks, I love you.

Marco Antonio C. Santos Novembre 22, 2007 at 12:48 matin

Great jQuery plugin. Really awesome. Do you have an example using Form Validation(required fields)? Your demo haves an example without required fields. Cheers

stan@pimentech Novembre 28, 2007 at 11:27 matin

I have fixed the demo' form to use required fields.

Marco Antonio C, Santos Novembre 29, 2007 at 3:48 après-midi

Your demo page is 404 error(page not found).

stan@pimentech Novembre 29, 2007 at 3:53 après-midi

Oups,
should works now, thanks.

test Décembre 8, 2007 at 1:31 après-midi

sd

Sean Décembre 10, 2007 at 7:18 matin

Is there a way to implement a back feature, whether using the browser back button or a link in the frame? I searched around a little on the web and could not find anything about this.

Great plugin, thanks.

fredz Décembre 12, 2007 at 1:10 après-midi

Back feature would be useful, but we'd have to implement a stack with url history + attached jframe. I have no time for the moment. If someone is interested to implement that, let me know.
Thanks for your comment.

Alexander Décembre 19, 2007 at 1:16 matin

I really like the script, good job! I love that it isn't overkill like a lot of other jQuery-plugins I've seen. But I agree on that a back-feature would be very useful.

Well, it's not not all problem-free. I've got a problem that I can't seem to solve. I made an example to show you what the problem is. Please take a look at http://help.alleballe.nu/.

I would really appreciate help with it..

Mr Window Décembre 20, 2007 at 2:45 après-midi

First of all, nice script. Really nice. Quick and easy like it sais really no hassle it almost feels like an addition to the HTML language.

I have one little problem though (not too serious for my use). When I try to load an external link (say google) it opens in a new window/tab and not in the target div. Perhaps I'm doing it wrong, I'm not too much of a JavaScript genius. I tried http://www.google.com in the href and without the http both without succes.

I'm not using that though but perhaps it would be nice for the future. Anyway, very nice script thanks for the good work!

fredz Décembre 20, 2007 at 3:03 après-midi

Mr Window, your problem is ajax security related : you can't do an external http request with javascript.

fredz Décembre 20, 2007 at 3:13 après-midi

Alexander, your form tag is not closed !

Roy Décembre 28, 2007 at 10:55 matin

I have created a page that contains a form and a $(document).ready function.

When the page is loaded and the ready function is called the form collection is empty.

I moved the script that initialize the form from the ready function to the end of the page and now the form collection is not empty and it works fine.

Either I am missing something (probably as I am new to jQuery) or there is bug - I thought that ready is called when the document is initialized and ready to be processed.

fredz Décembre 28, 2007 at 11:01 matin

Could you give urls for the 2 cases ?

Dado Janvier 4, 2008 at 7:40 matin

Can jFrame loaded pages go across domains?

james_027 Janvier 9, 2008 at 7:33 matin

hi,

Thank you for this wonderful plug-in you've shared to the community. Recently I run into trouble, which is not a jFrame bug at all but it's because IE's caching. In jQuery we can call $.ajax() function with cache:false, but I don't know how to apply it in your plug-in

Thanks

Piere Janvier 21, 2008 at 12:55 après-midi

Hi,

you plugin is awesome, you did there a good job :)

i have a question i couldnt solve myself atm.

how can i possible accsess the jframe per flash? ive tryed to target the actionscript link to jFrame but actually it only loads in a seperate window.

maybe there is a way to javascript such an actionscript loadframe1();

how could i possible accsess the jframe per javascript function?

cheers
piere

fredz Janvier 21, 2008 at 2:44 après-midi

Piere,
the jframe could be loaded with :
$(element).loadJFrame(url, [callback function])

fredz Janvier 21, 2008 at 2:55 après-midi

james_027, have you tried ajaxSetup function ?

Piere Janvier 21, 2008 at 3:22 après-midi

Hi fredz, thank your for your response :)

Iam new to js, could you please descripe this api call on your example?

i would like todo something like this:
i see per firebug console that the site loads the content when i enter the site, but it doesnt loads the content to its frame :/

<script type="text/javascript">
function loadMyFrame(url,target){
jQuery(target).loadJFrame(url);
$(target).loadJFrame(url);
}
loadMyFrame('test.html', 'jFrame');
</script>

Krasi Janvier 27, 2008 at 10:44 après-midi

When I submit the newly generated form(in the target div) with 'Enter' in IE, it reloads the whole page with the result of the submitted script(so that everything else on the page has disappeared). If I use the submit button, though, it reloads in the place expected. Both 'Enter' and submit button work fine in FF. So what could be the case with IE?

fredz Janvier 29, 2008 at 4:03 après-midi

Don't know. Any IE users ?

Jeffrey Janvier 29, 2008 at 7:40 après-midi

Great API!! I have implemented this easily but I am having no luck getting my <div id=people_list> to refresh or GET after the form has Posted the update. In the example there is a POST and then a GET that refreshes the list. In mine I am only seeing a POST action. Any thoughts??

Thanks

Krasi Février 2, 2008 at 9:50 matin

Has anyone achieved file uploads? The Form plugin which JFrame is based on does support them.

Peter Février 8, 2008 at 7:20 matin

Can anyone take a look at this problem? Use firebug to see the problem:

http://72.18.207.149/english/taconite-te...

It uses the jframe plugin, and the taconite plugin. For some reason, somehow, jframe causes some kind of problem on how taconite receives the response back from the server.

The problem does not occur with regular jquery ajax calls. Only with jframe. Any ideas?

MrPicker Février 12, 2008 at 1:03 après-midi

Hi, you plugin is very good.

But, i have tired width link (http://www.google.com) in ""<a href="index.html" target="jframe2">"" instead of index.html, but i don't work... i see a blank page.
Can i insert a link instead of a local page?

Thanks

dlm Février 22, 2008 at 9:57 matin

Krasi, I have the same ie-goes-to-new-page problem on my practice build, too. The interesting thing is, the demo on this site works as it should both when you click on the submit button *and* hit the return key.

Unfortunately, my project requirements stipulate ie so I can't really ignore it. Did you find a solution?

ps... Great plugin fredz! Wish I would have found it months ago. Thanks!

Michael Sandoz Février 27, 2008 at 6:22 après-midi

Hello, your solution is awesome!

I added an add record form to your sample. How can I get the people list to update after insert.

地耍花样 Mars 6, 2008 at 4:59 après-midi

塔顶

raduen Mars 15, 2008 at 4:42 après-midi

Nice plugin,
But i can't get it to work with eg
sample: have a div called odd with src="#"but when i clik on the link <a href="upload.php" target="odd"> it keeps opening the page in a new window. (ie)

Dips Mars 31, 2008 at 2:12 après-midi

@raduen and others with this issue:

This works if the link is INSIDE the div. If it's outside such as in another separate navigation bar, it won't work.

I don't have a solution to this but here's an alternative:
http://javascript.internet.com/ajax/ajax...

:-)

Levy Avril 3, 2008 at 8:31 matin

Some one know How get the people list to update after insert.

Thanks

levy Avril 3, 2008 at 8:38 matin

<script type="text/javascript">

$("#people_list").loadJFrame($("#people_list").attr("src"));

</script> ????

stan@pimentech Avril 3, 2008 at 9:50 matin

That's correct.

ernesto Avril 5, 2008 at 2:43 après-midi

It's a great plgin. But I ave a problem: I am trying to pass some parameters in this way:
<a href="simple.php?af_feed=<?php echo $ff_params; ?>" target="jFrame1">my_link</a>
And it does not work propertly.

michi Avril 10, 2008 at 12:49 après-midi

I don't think that it`s possible to get just on div from a cross-domain, is it?

I think that is prevented by the cross side scripting restrictions, isn't it?

michi Avril 10, 2008 at 12:52 après-midi

on = one or single....

I'm think of somthing lilke this: http://developer.apple.com/internet/webc...

Tony Avril 11, 2008 at 6:51 matin

Hi,

Trying to get the jFrame plug-in to work but unsure how to use it.

I have set-up the follow div, i.e:

<div id="content" src="#"></div> and then make a call to:
jQuery("#content").loadJFrame(returnURL);

The div loads successfully but I get a javascript error:

Line: 200364044
Char: 3
Error: Object doesn't support this property or method

Can someone please let me know what I have missed and please correct/show me the correct usage of this plug-in.

Thanks.
Tony.

trifide Avril 28, 2008 at 12:50 après-midi

what about a confirm button? if I try to put a confirm alert in the submit button, the alert pops up, but next, if I click on "cancel", it doesn't stop and sends the form... ( it doesn't pass the "false" to the onclick/onsubmit function ... ) maybe there is a simple workaround for this but ... thanks in advance;

aaron Mai 15, 2008 at 1:54 matin

I have a problem that's a more complicated version of what's illustrated in the index.html example file above.

Basically, I can't get the submit button to update a DIV. It doesn't seem to work in my test or in the example file. The 'submit' button fire, but doesn't update jframe2.

Has anyone else had or seen this problem?

I'm using:
jquery-1.2.3.min.js
jquery.form.js - 2.10
query.frame.js 1.0.1

Thank you.
aaron.

Comments