0

When I moved a paragraph of javascript code from a php file to a individual js file, the js doesn't work anymore.

I just simplely removed the "script" tag and paste all the js code into js file. Did I missing some codes or tags?

The code is

<script>
_i = <?=$k+1?>;

function  addlng() {
_id = "lang_"+_i;
$j("#lang_area").append("<div class='lang_addition' id="+_id+"></div>");
$j("#s_lang").clone().appendTo("#"+_id);
$j("#"+_id).append('<span><a href="javascript:void(0)" >&nbsp;<img id="addlng" class="addlng" 
src="<?=style_url('mvl/images/').'bullet_blue_collapse.png'?>" 
onclick="javascript:sub_img(\''+_id+'\');" /></a></span>');
_i++;

}

function sub_img(obj) {
$j("#"+obj).remove(); 

}
</script>

1 Answer 1

2

<?=$k+1?> is php. It outputs the value currently in $k+1. You'll have to look in the php to see what $k is. You'll likely need to keep this in your php file:

<script type="text/javascript">
    var _i = <?=$k+1?>;
</script>

Then remove that line for your .js file and make sure that you include your .js file later in your HTML than that script outputted by php.

3
  • The suggested solution works, but could be improved by making addLng function take an index instead of relying on a global. Otherwise, I don't see any reason to pull the JS out of the PHP code since they are still tightly coupled Commented Sep 27, 2011 at 22:46
  • @Juni: still not working is not a very useful comment, what's the error? @PaulPRO: Caching is a good reason, do I think it's good enough? If you're bundling all your JS, then for sure, otherwise, it may be a wash considering it's an extra HTTP request, at least to check it it's modified. Commented Sep 28, 2011 at 0:59
  • I found that there is still a php link "src="<?=style_url('mvl/images/')" over there. That's the problem. Thanks for helping~
    – Juni
    Commented Sep 28, 2011 at 2:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.