Home Web Front-end HTML Tutorial URL representation in HTML web pages_HTML/Xhtml_Web page production

URL representation in HTML web pages_HTML/Xhtml_Web page production

May 16, 2016 pm 04:42 PM
html url

In HTML, common URLs have many representations:
Relative URL:

Copy code
The code is as follows :

example.php
demo/example.php
./example.php
../../example.php
/example.php

Absolute URL:

Copy the code
The code is as follows:

http://jb51.net/example.php
http://jb51.net:80/example.php
https://jb51.net/example.php

At the same time, there are a large number of element attribute values ​​​​in HTML that are URLs. Generally, there are two methods to obtain these URL attribute values ​​using JavaScript:

Copy code
The code is as follows:

<a href="example.php" id="example-a">The absolute URL of the page at this time is http://jb51.net/ </a>
<script>
var oA = document.getElementById('example-a');
oA.href == 'http://jb51.net/example.php' ;
oA.getAttribute('href') == 'example.php';
</script>

We hope to get the complete absolute URL by directly accessing the attribute. Get its original attribute value through the getAttribute method. In fact, this is a relatively ideal result. Among all A-level browsers, only Firefox and IE8 can successfully obtain this result. Other browsers have more or less special circumstances. , please see the demonstration example for specific attributes of which elements exist.
The problem in most browsers is that both methods return the original attribute value, but in actual applications, what is often needed is its absolute URL. The solution in "Dealing with unqualified HREF values" is too Too complicated, here is a relatively simple solution, which will be very simple if you do not consider the difference in browser code:
<form action="example.php" id="example-form">
This The absolute URL of the page is http://jb51.net/</form>

Copy the code
The code is as follows:

<script>
var oForm = document.getElementById('example-form');
//IE6, IE7, Safari, Chrome, Opera
oForm.action == 'example.php';
oA.getAttribute('action') == 'example.php';
//General solution for getting absolute URL
getQualifyURL(oForm,'action') == 'http://jb51.net/example.php';
getQualifyURL = function(oEl,sAttr){
var sUrl = oEl[sAttr],
oD,
bDo = false ;
//Is it a version before IE8
//http://www.thespanner.co.uk/2009/01/29/detecting-browsers-javascript-hacks/
//http:/ /msdn.microsoft.com/en-us/library/7kx09ct1(VS.80).aspx
/*@cc_on
try{
bDo = @_jscript_version < 5.8 ?true : @false;
}catch(e){
bDo = false;
}
@*/
//If it is Safari, Chrome and Opera
if(/a/.__proto__==' //' || /source/.test((/a/.toString ''))
|| /^function (/.test([].sort)){
bDo = true;
}
if(bDo){
oD = document.createElement('div');
/*
//The result of the DOM operation will not change
var oA = document. createElement('a');
oA.href = oEl[sAttr];
oD.appendChild(oA);
*/
oD.innerHTML = ['<a href="' ,sUrl,'"></a>'].join('');
sUrl = oD.firstChild.href;
}
return sUrl;
}
&lt ;/script>

There are some more interesting things about the two prehistoric browsers IE6 and IE7. The attribute values ​​obtained by the two methods in the HTML elements A, AREA and IMG are Absolute URL. Fortunately, Microsoft provides a second parameter for getAttribute to solve this problem. At the same time, it can also solve the problem of the two methods mentioned above that both return the original attribute for IFEAM and LINK elements:

Copy code
The code is as follows:

<link href="../../example.css" id="example-link">
<a href="example.php" id="example-a">此时页面绝对URL是http://jb51.net/</a>
<script>
var oA = document.getElementById('example-a'),
oLink = document.getElementById('example-a');
oA.href == 'http://jb51.net/example.php';
oA.getAttribute('href') == 'http://jb51.net/example.php';
oA.getAttribute('href',2) == 'example.php';
oLink.href == 'example.php';
oLink.getAttribute('href') == 'example.php';
oLink.getAttribute('href',4) == 'http://jb51.net/example.php';
</script>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

Nested Table in HTML

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Table Border in HTML

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

HTML margin-left

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

HTML Table Layout

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Moving Text in HTML

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

HTML Ordered List

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

HTML onclick Button

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

How do you parse and process HTML/XML in PHP?

See all articles