NeoSer
NeoSer

Reputation: 519

Html anchor in another html

Suppose I have two html files footer.html and main.html. The footer contains a reference to the top of a page as follows:

<!-- footer.html -->

<!doctype html>
<html lang="en">
<head></head>
<body>
  <footer>
    <small><a href="_parent#header">To top</a></small>
  </footer>
</body>
 </html>

The main.html file embeds the footer by using the <object> tag (see note 1) as shown below. There can be several files similar to main.html. Because of this <a href="page#header"> may not be used.

<!-- main.html -->

<!doctype html>
<html lang="en">
<head></head>
<body>
  <div id="header">...</div>
  <div id="content"> Long content ... </div>
  <object id="footer" type="text/html" data="footer.html"></object>
</body>
</html>

Question: Is it possible to refer to the anchor from the footer to main without using javascript, php etc?

Note 1: The <object> tag can be used to embed another html, although without a relation:

You can also use the <object> tag to embed another webpage into your HTML document. from http://www.w3schools.com/tags/tag_object.asp

The same can be done using <iframe> or <embed> instead of <object>, but the issue remains.

Upvotes: 1

Views: 104

Answers (2)

NeoSer
NeoSer

Reputation: 519

Thanks to all for the comments and answers. Indeed, this approach seems not to work due to a missing relation between html files. In other words, footer.html cannot refer to the inside of main.html. Instead, I modified the structure, so that the main includes the footer directly and the content is embedded by using an <iframe> as follows:

<!-- main.html -->

<!doctype html>
<html lang="en">
<head></head>
<body>
  <div id="header">...</div>
  <iframe id="content" name="contentframe" src="content.html"></iframe>
  <footer id="footer">
    <small><a href="#" target="_self">To top</a></small>
  </footer>
</body>
</html>

This solves the issue and works without JS, PHP or the like independently of the page loaded into the <iframe>. That is, it simply jumps to top keeping the contents of the loaded page. Eventually, there is one main and multiple long content pages which are loaded into this main. Tested with FF and IE.

Upvotes: 0

Quentin
Quentin

Reputation: 943142

Is it possible to refer to the anchor from the footer to main without using javascript, php etc?

No, it isn't.

If you use a relative URL, then it will be relative to the document that the link appears in (i.e. the footer).

If you use an absolute URL, then you have to specify which document you want to link to the top of (and since multiple documents will embed the footer, you can't do that).

You've ruled out generating the URL programatically with JavaScript.

Upvotes: 1

Related Questions