更改变量名后JS脚本无法正常工作

时间:2015-08-10 20:32:25

标签: javascript

我正在为客户工作。我写了一些javascript,为客户打印一个链接列表,但它不像它应该的那样工作。您可以找到文件here。它应该打印三列,但它只打印前两列。在我改变一些变量名之前,这曾经工作过。什么可能出错?请参阅grizfirestarters.com/stores_dyn.htm

上的完成页面

编辑:我通过设置列大小而不是动态来解决问题。有点好玩,但完成工作。代码如下:

/****************
Author: Timothy Grindall
Copyright: Timothy Grindall 2008

Note: the array "city_name_wash" is passed to this script by another script that is loaded on the same page and does not need to be included in this script.
Liscense: GNU Public Liscense. Note: if you don't know what the GNU public liscense is, you can probably find out by searching in Wikipedia at "http://www.wikipedia.org". A GNU Public Liscense basically means that you can use this code in any way you like as long as you don't sell the whole or any part of it in the original javascript or in a translated and/or compiled form.
*********************/

/**************************
*    format and print     *
**************************/

// *** Start Washington stores *** //

wash_col_size = Math.floor(city_name_wash.length / 3);

add_count = 2;

// calculate difference between rounded wash_col_size and unrounded wash_col_size. If there is a difference we need to compensate for extra cities/entries by making slightly longer columns

if ( (city_name_wash.length - (wash_col_size*3)) != 0 ) {
    wash_col_size = wash_col_size + add_count;
}

document.write("<div id='cities_wrapperL'>");//begin cities_wrapperL

for (i=0; i < wash_col_size*2; i++) {

    // prepare a link name from the city name
    link_name = city_name_wash[i].split(" (")[0]; // finds the first part of the name minus the "()"s on the end

    document.write("<div id='cityL'><a href='fixjava.htm' onclick='javascript: window.location = \"store_info_map.html?img=\" + this image + \"&city = " + link_name + "\"; return false;'> + city_name_wash[i] + "</ a></div>");

    i++; // increment counter

    // prepare a link name from the city name
    link_name = city_name_wash[i].split(" (")[0]; // finds the first part of the name minus the "()"s on the end

    document.write("<div id='cityC'><a href='fixjava.htm' onclick='javascript: window.location = \"store_info_map.html?img=\" + this_image + \"&city=" + link_name + "\"; return false;'>" + city_name_wash[i] + "</ a></div>");

}

document.write("</div><div id='cities_wrapperR'>"); // end of cities_wrapperL and start of cities_wrapperR


for ( i = wash_col_size*2; i < wash_col_size*3; i++){

    // prepare a link name from the city name
    link_name = city_name_wash[i].split(" (")[0]; // finds the first part of the name minus the "()"s on the end

    document.write("<div id='cityR'><a href='fixjava.htm' onclick = 'javascript: window.location = \"store_info_map.html?img = \" + this_image + \"&city = " + link_name + "\"; return false;'> + city_name_wash[i] + "</ a></div>");

}

document.write("</div>"); // end of cities_wrapperR

// *** start Idaho stores *** //


var content = document.getElementsByName("content")[0];
content.id = "short-back" + (wash_col_size - 3); // set to id that has the same positioning, but a different background image
if ( wash_col_size < 4 ) content.id = "short-back"; // in case there is three or less
if ( wash_col_size > 9 ) content.id = "main-content_stores"


/**************************
*     target example:     *
**************************/

/*
<div id="cities_wrapperL">
    <div id="cityL"><a href="Aberdeen.htm">Aberdeen</a></div>
    <div id="cityC"><a href="Gold Bar.htm">Gold Bar</a></div>
    <div id="cityL"><a href="Anacortes.htm">Anacortes</a></div>
    <div id="cityC"><a href="Granite_Falls.htm">Granite Falls</a></div>
    <div id="cityL"><a href="Arlington.htm">Arlington (2)</a></div>
    <div id="cityC"><a href="Hoodsport.htm">Hoodsport</a></div>
    <div id="cityL"><a href="Bellingham.htm">Bellingham (2)</a></div>
    <div id="cityC"><a href="Joyce.htm">Joyce (2)</a></div>
</div>
<div id="cities_wrapperR">
    <div id="cityR"><a href="Port_Angeles.htm">Port Angeles</a></div>
    <div id="cityR"><a href="Port_Townsend.htm">Port Townsend (2)</a></div>
    <div id="cityR"><a href="Poulsbo.htm">Poulsbo (2)</a></div>
    <div id="cityR"><a href="Randle.htm">Randle</a></div>
</div>
*/

1 个答案:

答案 0 :(得分:0)

  

在我更改一些变量名之前,这曾经工作过。什么可能出错?

只是猜测,你填补了更改变量名称 - 更改它们,然后重新开始,或者再次触摸它们,取决于你的感受 - 看代码,它看起来比2008版权更旧约会,我建议将代码踢到现代的尖叫和

或者,在您喜欢的任何浏览器中查看开发人员控制台,并看到第45行有一个未定义的变量,即#include <cassert> template<class A, class B> bool f() { return true; } int main() { assert(f<char, int>()); }