www.webdeveloper.com
Results 1 to 6 of 6

Thread: Adding a Link element to the header

  1. #1
    Join Date
    Jan 2010
    Posts
    3

    Talking Adding a Link element to the header

    I need to be able to add a link element to the head element through a separate file then the file containing the head element. My attempt was as follows:

    Code:
    document.getElementsByName("head").innerHtml += "<link rel=\"image_src\" href=\"<?php echo $img;?>\"/>";

  2. #2
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    104
    I think you meant

    var arr = document.getElementsByTagName('head');

    And that returns an array which you need to walk through like this.

    for (i = 0; i < arr.length; i++) {
    arr[i].innerHTML += "something";
    }

  3. #3
    Join Date
    Jan 2010
    Posts
    3
    would that return an array of all the elements in the head?

    If so would
    Code:
    arr[] = <link href=""/>;
    add a link tag to the head?

  4. #4
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    104
    No.
    It would return an array of <head> elements. There should only be one of those.

    .innerHTML returns a string of everything between, but not including, the <head> and </head> tags. You may have to put it in a variable then send it back out like this.

    var innerhtml=arr[0].innerHTML;
    innerhtml += "something";
    arr.innerHTML = innerhtml;

  5. #5
    Join Date
    Jan 2010
    Posts
    3
    Ok on the last line arr.innerHTML = innerhtml;, that doesn't set the actual head innerhtml I still need a line of code for that right?

  6. #6
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    104
    First of all I miss typed it. It should be.

    arr[0].innerHTML = innerhtml;

    What that would do is replace everything in the <head> section with whats in innerhtml.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles