开发者

is any difference between running <script></script> and function() javascript?

``hi,

using whizzywig wysiwyg editor y found that having enter code here makeWhizzyWig("edited", "all"); is ok

but running

<script>
function doit(){
      makeWhizzyWig("edited", "all");
}
</script>

doit()

it breaks....

any suggestion about this difference? also i will appreciate some kind of explanation about this

thanks in advance makerjoe

-------------------------------------------------------------------------------------------

as per your request 

this works ok

<head>
<script type="text/javascript" src="/makerjoe/js/whizzywig.js"></script>
</head>

<body>
<form name="Whizzy" action="whizzed.php" method="post" onsubmit="syncTextarea();">
<textarea name="edited" id="edited" rows="15" cols="70" style="width:99%; height:500px;"> 
</textarea>
<input type="submit" name="submit" value="Submit" title=" Displays your page, which you can Save from the File menu. ">
</form>

<script>
      makeWhizzyWig("edited", "all");
</script>

</body>



------------------------------------------------------------------开发者_Python百科------------------------

the following does not work!!!

<head>
<script type="text/javascript" src="/makerjoe/js/whizzywig.js"></script>
</head>

<body>
<form name="Whizzy" action="whizzed.php" method="post" onsubmit="syncTextarea();">
<textarea name="edited" id="edited" rows="15" cols="70" style="width:99%; height:500px;"> 
</textarea>
<input type="submit" name="submit" value="Submit" title=" Displays your page, which you can Save from the File menu. ">

</form>
<script>
function doit(){
      makeWhizzyWig("edited", "all");
}
</script>

<a href=javascript:doit()> doit </a>

</body>


We couldn't see the issue because of your formatting on the question, but after editing it to add code tags for you, I noticed that you are defining your function outside of your <script> tag. The following should work:

<script>
    function doit(){
        makeWhizzyWig("edited", "all");
    }

    doit();
</script>


Functions are just a way of grouping statements of code together in a way that makes them easier to call and reuse.

In your example doit() only contains one statement and contains no parameters so the value of it is questionable, you may as well just call makeWhizzyWig directly. However, in most cases functions are a vital part of structuring and organising code.


The code you are showwing it should work. Or at least, the method doit() should get called when you click on the doit link. If you add an alert call at the beginning of the doit method you should see the popup window appear when you click on the link. Do you ?

function doit(){
    alert('should be reached');
    makeWhizzyWig("edited", "all");
}

If you do not see the alert window, what browser are you using ?

Edit

After seeing that it worked, I created a js file:

test.js
function makeTest(param1, param2)
{
    alert("first: " + param1 + " second: " + param2);
}

and modified my html file:

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

<body> 


<script> 
function doit()
{
      makeTest("edited", "all");
}
</script> 

<a href=javascript:doit()> doit </a> 

</body> 

This shows that the problem is not in the way you are calling the doit function but that there is a problem inside the makeWhizzyWig() when it is called from a global context. Can't say much more since I do not have the source to makeWhizzyWig but you could try changing

<a href=javascript:doit()> doit </a>

to

<a href="#" onClick="doit()"> doit </a>

and see if it works


makeWhizzyWig("edited", "all"); creates a new Whizzywig using document.write. It has to be called inline before the page has loaded. Your problem is that you are not calling your doit() function until after the page has finished loading, so the document.write inside makeWhizzyWig won't work because the document is already complete.

See http://unverse.net/WYSIWYG and click "Dynamic Loading" for an example of how to dynamically create editors with Whizzywig.

Or try Whizzywig 2011, which solves this problem by not using document.write

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜