Problems with elements that do not support static expando in HTML _javascript tips

Source: Internet
Author: User
When using a script to unify a batch of pages, I want to get the title of the document and some of my custom attributes by the title element of the page being processed. These additional properties are added to the server by the attributes collection, but the results of the run are always far from my expectations, how do I write custom attributes in the title element that are always empty?

Customer service end and server-side code are:

ASPX:
<title id= "title" runat= "Server" >query info</title>
C #: protected HtmlGenericControl title;

private void Page_Load (object sender, System.EventArgs e)
{
// ...
Title. InnerHtml = "Query Information";
Title. attributes["icon"] = "queryinfo.gif;"
}
Html:
<title id= "title" icon= "Queryinfo.gif" > Query Information </title>
Javascript:
var iconpath = docucment.all.tags (' TITLE ') [0].icon;
if (IconPath)
{
var img = document.createelement (' img ');
IMG.SRC = IconPath;
// ...
}

The result is always out of the I hope that icon, tracking JavaScript code, found IconPath always undefined. Look at the HTML code, which clearly has icon= "queryinfo.gif" this attribute value pairs. So from the title object to see Outerhtml,outerhtml incredibly is: <title id= "title" > Query Information </title>. There is no icon that attribute, no wonder that the iconpath of JavaScript is always undefined.

After careful investigation, it turns out that HTML elements are different in processing expando properties. There are two ways we can add expando attributes to HTML elements, one is the dynamic way we use them, and that is, using scripts to add expando attributes to HTML elements. The other is to use a static method, that is, to add the expando attribute literal in the HTML code. Examples are as follows:

Dynamically adding expando properties: <span Id=myspan>this is a span element.</span>
<script language=javascript>
Myspan.myattri = ' attribute ';
</script>
Static Add expando property: <span Id=myspan Myattri=attribute>this is a span element.</span>
Originally these two kinds of writing basically is no difference, but the element title only support dynamically add expando property, and not support static add expando attribute, which is why I started to make the wrong reason. So what about the HTML elements and the title, which doesn't support statically adding expando properties? In 108 HTML elements, the following 10 elements do not support statically adding expando properties:
HTML, head, TITLE, body, Basefont, FORM, HR, HR, TBODY, SCRIPT
However, dynamically adding the expando property is supported by all HTML elements and DHTML objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.