开发者

Create a javascript file to have <object> or <embed> tags in it for use in various html pages

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>  


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)
;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜