0

I'm using PHP, HTML & jQuery. Lets say this is my original working "index.html"

<script type="text/javascript" src="jquery.js"></script>
<script>
var p1 = "This is part 1";
var p2 = "This is part 2";
$(document).ready(function(){
    alert(p1);
    $('#content').html(p2);
});
</script>
<div id="content"></div>
<div id="content2">Hello</div>

I want to break the code into "index.html" & "otherFile.notSureWhatExt" but they are working as if in the same file.

index.html:

<script type="text/javascript" src="jquery.js"></script>
<script type="notSureWhatType" src="otherFile.notSureWhatExt"></script>
<script>
var p1 = "This is part 1";
$(document).ready(function(){
    alert(p1);
});
</script>
<div id="content"></div>

otherFile.notSureWhatExt:

<script>
var p2 = "This is part 2";
$(document).ready(function(){
    $('#content').html(p2);
});
</script>
<div id="content2">Hello</div>

Using jsp, I can simply use <%@ include file="otherFile.anyCustomExtAlsoWorks"%>. But here I don't now. What I want is when calling index.html, I can see message box "This is part 1" and 2 div at the bottom which is "This is part 2" & "Hello".

Coisox
  • 1,002
  • 1
  • 10
  • 22

3 Answers3

2

You can put your javascript in a .js file. The script tags are not needed.

You can then include it with

<script type="text/javascript" src="otherFile.js"></script>
mbuurman
  • 86
  • 1
0

The extension should be .js and remove the <script>-tags from that file.

The type is text/javascript - but it is not necessary, as it is ignored by the browsers.

Magnar
  • 28,550
  • 8
  • 60
  • 65
0

I think it can be done via "server-side includes" . any way you are using php so you can make use of php. But here the file1.php or file1.html can include in file2.php. That is a php/html file can include in a php file but not one html in another html. You can go through the following links : http://webdesign.about.com/od/ssi/a/aa052002a.htm and http://www.boutell.com/newfaq/creating/include.html and even one discussion has happened in How to include one HTML file into another?

Community
  • 1
  • 1
Reddy Prasad
  • 251
  • 2
  • 6