views:

45

answers:

3

I have more than 200 pages with the same <object> or <embed> tags for. I need to have a .js file to have this repeatative information in it. This is the tag is repeating in all 200-300 page:

<OBJECT width="720" height="540">  
<PARAM NAME="Src" value="../pdf/sign-va.pdf">   
<embed width="720" height="540" src="../pdf/sign-va.pdf"
       href="../pdf/sign-va.pdf"></embed></OBJECT>  
A: 

Firstly, Marcel Korpel is right: using a server-side technology to include this snippet is the smarter way to go. If that isn't an option for you for whatever reason, you can do this:

To insert it using Javascript, you'd firstly need some way to identify where to put it. You could do this by having a div with a certain ID, or always put it in the same place (e.g.: at the end of the <body>).

var filename = '../pdf/sign-va.pdf',
    w = 720,
    h = 540
;
var obj = document.createElement('object');
    obj.setAttribute('width', w);
    obj.setAttribute('height', h);
    var param = document.createElement('param');
        param.setAttribute('name', 'Src');
        param.setAttribute('value', filename);
    obj.appendChild(param);
    var embed = document.createElement('embed');
        embed.setAttribute('width', w);
        embed.setAttribute('height', h);
        embed.setAttribute('src', filename);
        embed.setAttribute('href', filename);
    obj.appendChild(embed);
// here is where you need to know where you're inserting it

// at the end of the body
document.body.appendChild(obj);

// OR, into a particular div
document.getElementById('some_id').appendChild(obj);

If you were using something like jQuery, this becomes much less verbose:

$('<object></object>', { width: w, height: h})
    .append($('<param />', { name: 'src', value : filename }))
    .append($('<embed></embed>', {
        width: w, height: h,
        src : filename, href : filename
    }))
    .appendTo(document.body)
;
nickf
1) Don't use `setAttribute` to set those properties. For an explanation, see [Bobince's comments below this answer](http://stackoverflow.com/questions/2775328/alt-attribute-encoding-with-javascript/2775436#2775436). 2) Just curious: why do you put those semicolons on new lines? Aren't `h = 540;` and `.appendTo(document.body);` just neater?
Marcel Korpel
@Marcel - it's just a style I've picked up since working with jQuery. Since you do everything on "one line", I kinda treat the semicolon like I would a closing bracket. It lines up with the start of that statement, and doesn't leave it hanging.
nickf
Dear Nickf and Marcel, I am a very slow learner. I think I missed one big thing in my question that all the pdf, txt, dwf, jpg files (../pdf/sign-va.pdf,../pdf/sign-va1.txt, ../pdf/sign-va2.jpg, and goes on...) are different in each of 200 pages, but only the size is the same 720x540. and I have to use 'object' and 'embed' in order to work. Please let me know how to modify the code. And I will be using div.Thanks again! You guys are greatar than my collage teachers! And I am just way slower that my college time...
Jay
A: 

Thanks again! You guys are greatar than my collage teachers! And I am just way slower that my college time...

jay
A: 

Dear Nickf, I tested your code here: http://asoon.com/test/3.htm

It works in Firefox, Safari, Opera, & Chrome, But ie7 does not open it. Please advise. Thanks!

jay
Please don't post questions or comments as **answers**. They get lost in noise. This isn't a forum or so. To update a question, click `edit` link below question. To post a comment, click `add comment` button. Also see http://stackoverflow.com/faq
BalusC
I tried, but my ie7 browser does not show the "add comment" or "edit" link.
jay