views:

32

answers:

2

Hello everyone!

I am just learning to create websites and I have been programmming 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.

+1  A: 

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.

Sarfraz
Thx a lot this is precisely the answer that I was looking for.What are the pros and cons with using server side vs client side includes?I know that content inside javascript are not detectable by searchengines but I wanted this mostley to include things like my loginform that I don´t want to rewrite every time I create a new site.
Narancha
@Narancha: You should use server-side includes they are secure as better. Javascript can be edited/fooled by bad guys easily.
Sarfraz
+1  A: 

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.

MvanGeest
Seconded. This method has too many drawbacks for users and search engines.
Pekka
pekka can you explain a litle further? why should I don't use server side includes?
Narancha