Jump to content

jQuery - load HTML fragments?


Perad

Recommended Posts

Hello, could someone help me out here.

 

I have a login button which takes the user to the login page. Now... this to me seems like a slow way of logging in. I would like users who have javascript enabled to click the login button. Instead of them being forwarded to the login page, I want the login form to magically pop up on top of the web page.

 

I can do this, I can creative a div, add the html and probably with a bit of trying - get the user to login.

 

However I have a question. For maintainability, I want to have create a separate HTML file, how would I insert this into my page using Javascript?

 

Furthermore speed is crucial, is there a way to have this fragment load in the background on every page? I am not sure what would be best, i just don't want a second or two wait when the button is pressed.

 

Basically - in short - how to I load this file fragment? What is the fastest way to do this?

Link to comment
Share on other sites

preLoad:

 


<div id="loginHolder" style="display:none;">
</div>

<div id="loginButton">
<noscript><a href="/login/">Login</a></noscript>
</div>

<script (js)>

$(document).ready(function(){

$("#loginHolder").load('loginpage.html');
$("#loginButtom").html('<a href="#" onclick="toggleLogin();">Login</a>');

});

function toggleLogin(){

if($("#loginHolder").css("display")=="none"){

$("#loginHolder").css("display","block");

}else{

$("#loginHolder").css("display","none");

}//end if

}

</script>

 

popup

 

 


<div id="loginHolder" style="display:none;">
</div>

<div id="loginButton">
<a href="#" onclick="$("#loginHolder").load('loginpage.html');">Login</a>
<noscript><a href="/login/">Login</a></noscript>
</div>


});

</script>

 

Of course this is untested for syntax errors, etc, and it is reliant on jquery...

 

I'd say there really shouldnt be a difference in preloading an html file or ajaxing it, as long as your users have normal connection speed

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.