最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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>  

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>  
Share Improve this question edited Jun 29, 2010 at 0:43 Jamie Wong 18.4k8 gold badges66 silver badges81 bronze badges asked Jun 29, 2010 at 0:25 JayJay 211 silver badge2 bronze badges 2
  • Why do you want this in JavaScript? I'd rather put these elements in your document use a server-side script. – Marcel Korpel Commented Jun 29, 2010 at 0:48
  • You might want to consider using server side includes if your server offers them. You could even write a program to modify all your pages. See en.wikipedia/wiki/Server_Side_Includes to get started with server side includes. – DaveB Commented Jun 29, 2010 at 0:59
Add a ment  | 

1 Answer 1

Reset to default 4

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 bees 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)

  1. 暂无评论