0

Is there a way to embed a class from another page in my site? Lets say I have this code in the page "index.html"

<div class="cls1">
  content of div...
</div>

and I want to embed it to another page "anotherpage.html" in my site. So I do:

<iframe src"?"></iframe>

What should i put instead the ?

note I want to take from the first file only a divs with the same class

boltan
  • 11
  • 2
  • 4
    Server side includes (assuming your hosting provider supports it) would be the best solution. Failing that, AJAX would work too. – Rory McCrossan Dec 14 '17 at 13:25
  • Possible duplicate of [Include another HTML file in a HTML file](https://stackoverflow.com/questions/8988855/include-another-html-file-in-a-html-file) – Serge K. Dec 14 '17 at 13:26
  • Its not a duplicate because I want only certain class – boltan Dec 14 '17 at 13:32
  • If you mean you want to just add the div with class cls1 into your other page, you can't do that with an iframe (the iframe can only include the entire page), instead you'd have to go with one of Rory's suggestions coupled with some parsing to extract the specific div you require. – delinear Dec 14 '17 at 13:34
  • 1
    "embed a class" is not self-explanatory. What does it mean? – Roamer-1888 Dec 14 '17 at 13:42
  • I'm pretty confused by your phrasing. I interpret it to be that you just want to use a css class on page 2 that you use on page 1. So, what is preventing you from just putting the css class into its own ".css" file and using it for both files? – TheCrzyMan Dec 14 '17 at 13:46
  • He needs only the entire div with class cls1 to be embedded in anotherpage.html – Vinod Bhavnani Dec 14 '17 at 14:01
  • 1
    @TheCrzyMan I read it (from the example html provided) as: I have a div with a specific class that I want to use on another page. – freedomn-m Dec 14 '17 at 14:01

1 Answers1

1

You can separate your html to two files, and then add iframe to another file.

First file:

<div class="something">
Tralala
</div>

Second File:

<iframe src="/path/to/your/first/file.html">
</iframe>

iframe requires src (source) parameter, which should be a valid URL where html file lays. However you can use javascript to assume this type of behaviour.

Using jquery for example:

$('.div2').html($('.div1').html());
Lukáš Irsák
  • 1,092
  • 1
  • 14
  • 23