简体   繁体   English

多个 HTML 文件仅链接到一个 Javascript 文件

[英]Multiple HTML files linked to only one Javascript file

I want to have functions in an external Javascript file to be linked to each of my html files.我想在外部 Javascript 文件中使用函数链接到我的每个 html 文件。 However, I want some functions to be linked to one html file while other functions would be linked to a different html file.但是,我希望某些函数链接到一个 html 文件,而其他函数将链接到另一个 html 文件。 Do I need to have multiple Javascript files or can I condense them into only one file?我需要多个 Javascript 文件还是可以将它们压缩成一个文件? When I tried to use one Javascript file every function would run on each of my pages.当我尝试使用一个 Javascript 文件时,每个 function 都会在我的每个页面上运行。 Is there a way to call a specific function in html without having the whole code?有没有办法在没有完整代码的情况下调用 html 中的特定 function? Like with css using #example for the id example in html.与 css 一样,使用#example作为 html 中的 id example

I used this link in my index.html file:我在 index.html 文件中使用了这个链接:

<script type="text/javascript" src="example.js"></script>

While this one was in another html file:虽然这个在另一个 html 文件中:

<script type="text/javascript" src="example2.js"></script>

These links are the only Javascript included in my html files.这些链接是我的 html 文件中唯一包含的 Javascript。

I'm not sure if additional examples of Javascript or html code is needed since this seems like a general question.我不确定是否需要 Javascript 或 html 代码的其他示例,因为这似乎是一个普遍问题。 Although, if more code is require then please tell me what specifically and I will gladly add it.虽然,如果需要更多代码,请告诉我具体是什么,我会很乐意添加。 I'm also new to coding so if you could ask before downvoting I would greatly appreciate it.我也是编码新手,所以如果您能在投票前提出要求,我将不胜感激。

I think I found the problem.我想我发现了问题。 I have an RSS feed that comes through a free website but the function doesn't have anything next to it.我有一个来自免费网站的 RSS 提要,但 function 旁边没有任何内容。

The html file links to the Javascript file with this in it and because the function is blank it automatically runs or that's what i'm assuming. html 文件链接到 Javascript 文件,因为 function 是空白的,它会自动运行,或者这就是我假设的。

(function(){
var a=window;
var b="";
for(i=0;i<a.rssfeed_url.length;i++) {
    b=b+"rssfeed[url]["+i+"]="+encodeURIComponent(a.rssfeed_url[i])+"&"
}
var c="http://feed.surfing-waves.com/php/rssfeed.php"+"?"+b+"rssfeed[type]="+(a.rssfeed_type?a.rssfeed_type:"")+"&rssfeed[frame_width]="+a.rssfeed_frame_width+"&rssfeed[frame_height]="+a.rssfeed_frame_height+"&rssfeed[scroll]="+(a.rssfeed_scroll?a.rssfeed_scroll:"")+"&rssfeed[scroll_step]="+(a.rssfeed_scroll_step?a.rssfeed_scroll_step:"")+"&rssfeed[scroll_bar]="+(a.rssfeed_scroll_bar?a.rssfeed_scroll_bar:"")+"&rssfeed[target]="+(a.rssfeed_target?a.rssfeed_target:"")+"&rssfeed[font_size]="+(a.rssfeed_font_size?a.rssfeed_font_size:"")+"&rssfeed[font_face]="+(a.rssfeed_font_face?a.rssfeed_font_face:"")+"&rssfeed[border]="+(a.rssfeed_border?a.rssfeed_border:"")+"&rssfeed[css_url]="+(a.rssfeed_css_url?encodeURIComponent(a.rssfeed_css_url):"")+"&rssfeed[title]="+(a.rssfeed_title?a.rssfeed_title:"")+"&rssfeed[title_name]="+(a.rssfeed_title_name?a.rssfeed_title_name:"")+"&rssfeed[title_bgcolor]="+(a.rssfeed_title_bgcolor?encodeURIComponent(a.rssfeed_title_bgcolor):"")+"&rssfeed[title_color]="+(a.rssfeed_title_color?encodeURIComponent(a.rssfeed_title_color):"")+"&rssfeed[title_bgimage]="+(a.rssfeed_title_bgimage?encodeURIComponent(a.rssfeed_title_bgimage):"")+"&rssfeed[footer]="+(a.rssfeed_footer?a.rssfeed_footer:"")+"&rssfeed[footer_name]="+(a.rssfeed_footer_name?a.rssfeed_footer_name:"")+"&rssfeed[footer_bgcolor]="+(a.rssfeed_footer_bgcolor?encodeURIComponent(a.rssfeed_footer_bgcolor):"")+"&rssfeed[footer_color]="+(a.rssfeed_footer_color?encodeURIComponent(a.rssfeed_footer_color):"")+"&rssfeed[footer_bgimage]="+(a.rssfeed_footer_bgimage?encodeURIComponent(a.rssfeed_footer_bgimage):"")+"&rssfeed[item_bgcolor]="+(a.rssfeed_item_bgcolor?encodeURIComponent(a.rssfeed_item_bgcolor):"")+"&rssfeed[item_bgimage]="+(a.rssfeed_item_bgimage?encodeURIComponent(a.rssfeed_item_bgimage):"")+"&rssfeed[item_title_length]="+(a.rssfeed_item_title_length?a.rssfeed_item_title_length:"")+"&rssfeed[item_title_color]="+(a.rssfeed_item_title_color?encodeURIComponent(a.rssfeed_item_title_color):"")+"&rssfeed[item_border_bottom]="+(a.rssfeed_item_border_bottom?a.rssfeed_item_border_bottom:"")+"&rssfeed[item_source_icon]="+(a.rssfeed_item_source_icon?a.rssfeed_item_source_icon:"")+"&rssfeed[item_date]="+(a.rssfeed_item_date?a.rssfeed_item_date:"")+"&rssfeed[item_description]="+(a.rssfeed_item_description?a.rssfeed_item_description:"")+"&rssfeed[item_description_length]="+(a.rssfeed_item_description_length?a.rssfeed_item_description_length:"")+"&rssfeed[item_description_color]="+(a.rssfeed_item_description_color?encodeURIComponent(a.rssfeed_item_description_color):"")+"&rssfeed[item_description_link_color]="+(a.rssfeed_item_description_link_color?encodeURIComponent(a.rssfeed_item_description_link_color):"")+"&rssfeed[item_description_tag]="+(a.rssfeed_item_description_tag?a.rssfeed_item_description_tag:"")+"&rssfeed[no_items]="+(a.rssfeed_no_items?a.rssfeed_no_items:"")+"&rssfeed[cache]="+(a.rssfeed_cache?a.rssfeed_cache:"");
if(a.rssfeed_border!="off"&&!a.rssfeed_css_url){}
document.write('<iframe name="rssfeed_frame" width="'+a.rssfeed_frame_width+'" height="'+a.rssfeed_frame_height+'" frameborder="0" src="'+c+'" marginwidth="0" marginheight="0" vspace="0" hspace="0" scrolling="no" ALLOWTRANSPARENCY="true"></iframe>')
})()

The only problem now is that I don't know how to call the function. Another function I have is called because I have onClick="example()" which is fine for that but the RSS feed needs to load automatically.现在唯一的问题是我不知道如何调用 function。另一个 function 被调用是因为我有onClick="example()"这很好,但 RSS 提要需要自动加载。 I don't want to have to click a button to get the feed to appear.我不想必须单击按钮才能显示提要。

Functions only run if you call them. 只有在调用它们时才会运行函数。 So you can have one JS file that contains functions that are never called by one HTML file, and nothing goes wrong -- they have no impact on the functioning of the page. 因此,您可以拥有一个包含从未被一个HTML文件调用的函数的JS文件,并且没有任何问题 - 它们对页面的功能没有影响。 The only downside of extra unused code is that is takes longer to load, but that isn't really significant until the file gets very big. 额外未使用代码的唯一缺点是加载时间更长,但在文件变得非常大之前这并不重要。

You could have a file named "common.js" that holds all related JavaScript functionality. 您可以拥有一个名为“common.js”的文件,其中包含所有相关的JavaScript功能。 But you will need separate JavaScript (either in a separate file or embedded in the HTML page itself) to make specific use of the common functionality according to the needs of the different pages. 但是,您需要单独的JavaScript(在单独的文件中或嵌入在HTML页面中),以根据不同页面的需要特定使用通用功能。

After seeing some code, one may be able to provide a better answer. 看到一些代码后,人们可以提供更好的答案。

It's more logical to split your javascript code into multiple files where it is appropriate. 将javascript代码拆分为适当的多个文件更合乎逻辑。 But you don't have to run any code in your javascript file even if it is linked to your html page if you put your code into functions. 但是,如果将代码放入函数中,即使链接到html页面,也不必在javascript文件中运行任何代码。

function myFunction()
{
    // do something.
}

The code inside this function won't run unless you call it in your html. 除非你在html中调用它,否则此函数中的代码将不会运行。

<body onload="myFunction()">
  ...
</body>

I think you can do that for example by adding id or class to the html or body, and then, write conditions, if the element (html or body) contains the id/class execute the appropriate functions. 我认为你可以这样做,例如通过向html或body添加id或类,然后编写条件,如果元素(html或body)包含id / class执行相应的函数。 When are the functions called? 这些函数何时被调用? On page load? 在页面加载? If you provide sample code, it would be better. 如果您提供示例代码,那就更好了。

You can use location.pathname and check the path and use switch or if else您可以使用 location.pathname 并检查路径并使用 switch 或者 if else

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM