简体   繁体   English

如何使此弹出代码更整洁?

[英]How do I make this popup code cleaner?

This is how I'm currently creating a popup for a few links on my site. 这是我目前为网站上的一些链接创建弹出窗口的方式。

function popitup(url) {
  newwindow=window.open(url,'name','height=615,width=475');
  if (window.focus) {newwindow.focus()}
}

<a href="song-lyrics.txt" onclick="return popitup('song-lyrics.txt')"></a>

Is there a cleaner way to write this? 有没有更清洁的方式来写这个? Also, how do I make the scrollbar appear? 另外,如何使滚动条出现?

I'm not exactly sure what you mean by a cleaner way, the code seems fairly straight forward to me. 我不确定您用更简洁的方式表示什么,代码对我来说似乎很简单。 But you could get rid of the return in the onclick of the anchor tag since it's useless there and perhaps you could use underscore/camel case to name your function ( pop_it_up or popItUp ) for better readability. 但是,您可以摆脱锚标记的onclick中的return ,因为它在那里没有用,也许您可​​以使用下划线/驼峰式大小写来命名函数( pop_it_uppopItUp ),以提高可读性。

For scrollbars add ,scrollbars=yes to the last parameter (strWindowFeatures) in window.open. 对于滚动条,scrollbars=yes在window.open中的最后一个参数(strWindowFeatures)中添加,scrollbars=yes

Be sure to change newwindow= to var newwindow= ; 确保将newwindow=更改为var newwindow= ; otherwise you are declaring a global variable every time you run this function. 否则,每次运行此函数时都要声明一个全局变量。

Otherwise, I agree with fsong's answer that it's a pretty basic function and doesn't need much cleaning. 否则,我同意fsong的回答,它是一个非常基本的功能,不需要太多清洁。 Here's what I would do, but it's minor stuff: 这是我会做的,但这只是次要的事情:

function popItUp(url) {
    var newWindow = window.open(url, 'name', 'height=615,width=475,scrollbars=yes');

    if (newWindow.focus) {
        newWindow.focus();
    }
}

JSLint 杰林特

I would check your code through JSLint . 我会通过JSLint检查您的代码。

JSLint is a JavaScript program that looks for problems in JavaScript programs. JSLint是一个JavaScript程序,用于查找JavaScript程序中的问题。 It is a code quality tool. 这是一个代码质量工具。

Like author says JSLint will hurt your feelings, but I think the error you should certainly fix is: 就像作者所说的那样,JSLint会伤害您的感情,但是我认为您应该解决的错误是:

Problem at line 2 character 3: 'newwindow' was used before it was defined.

This is issue exactly like Domenic said. 就像Domenic所说的那样,这是一个问题。

When you check your code with JSLint your code will also have better success when using tools like Javascript Minifier . 当您使用JSLint检查代码时,使用诸如Javascript Minifier之类的工具时,代码也将获得更好的成功。

Make Javascript external 将Java设为外部

I also think you should make Javascript and CSS external . 我还认为您应该将Javascript和CSS设置为外部

Using external files in the real world generally produces faster pages because the JavaScript and CSS files are cached by the browser. 在现实世界中使用外部文件通常会产生更快的页面,因为JavaScript和CSS文件是由浏览器缓存的。 JavaScript and CSS that are inlined in HTML documents get downloaded every time the HTML document is requested. 每次请求HTML文档时,都会下载HTML文档中内联的JavaScript和CSS。 This reduces the number of HTTP requests that are needed, but increases the size of the HTML document. 这减少了所需的HTTP请求的数量,但增加了HTML文档的大小。 On the other hand, if the JavaScript and CSS are in external files cached by the browser, the size of the HTML document is reduced without increasing the number of HTTP requests. 另一方面,如果JavaScript和CSS在浏览器缓存的外部文件中,则可以在不增加HTTP请求数量的情况下减小HTML文档的大小。

I think you should read Yahoo!'s best practices, because it has a lot of very good tips to optimize your website(javascript). 我认为您应该阅读Yahoo!的最佳做法,因为它有很多非常好的技巧来优化您的网站(javascript)。

Why popups? 为什么弹出窗口?

But Why should you use popups? 但是为什么要使用弹出窗口? Some users block these, which renders your site unusable. 一些用户阻止了这些,使您的网站无法使用。 I would advice you to use JQuery and just load content into the DOM. 我建议您使用JQuery并将内容加载到DOM中。

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

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