开发者

insert external html

I am just learning to create websites and I have been program开发者_运维问答mming a lot of OOB languages before so I am kind of used to write small objects and just paste them where I want them.

I would like to know if there is a way to create for instace a login form och what ever piece of html that you use regulare on sites and save that to a file, html or xml and then with the help of javascript add this form onto your main site.

I will try to make an example to clearify what I want to do, it's the javascript that I do not know how to write...

form.html

< form id="form_login" >
Username: <input type="text" id="username"/><br>
Password: <input type="password" id="password"/><br>
<input type="button" id="button_login" value="log in" onclick="login(this)"/>
</form>

index.html

<html>
<head>
</head>
<body>
<div>

<script type="text/javascript" src="javascript.js"></script>

</div>
</body>
</html>

javascript.js

// this is where I am rendered clueless, I want my javascript to render out my form
$(#"div").html(form.html)

I am thinking that I should do a serverrequest to retrieve the form.html but I don't know how.


Personally, I believe that JavaScript is not very well suited for the job - people that turn off JavaScript (and Google too!) won't see important parts of your page (in Google's case, that can lead to less visitors!). Inclusions as you mention them should happen at the server. See Wikipedia's article on Server Side Includes for a possible solution.


Use load method instead:

$('#div').load('form.html', function() {
  alert('Load was performed.');
});

It is better to include your files through server-side languages as well using server side includes.


You may want to consider using jQuery templates. They're in beta but have proven to be very useful to keep HTML code, well, HTML. http://api.jquery.com/category/plugins/templates/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜