最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML onload - using variables as parameters - Stack Overflow

programmeradmin3浏览0评论

I want to use something like:

<body onLoad="init('A sentence with "quoted text" as parameter')">

Unfortunately, this does work, as the quotes in the parameter are not treated properly.

Escaping the quotes also does not work

<body onLoad="init('A sentence with \"quoted text\" as parameter')">

(Above also does not work).

How do I deal with this. I though maybe I can create a string variable and assigne my sentence (with quotes) to it. But I dont know how to do it! The body onload is HTML and the Javascript variables would be visible only within the scope of the script, right? To be precise, the following does not work:

<script language="JavaScript">
var dada='A sentence with \"quoted text\" as parameter';
</script>
<body onLoad="init($dada, '</a>')">

I want to use something like:

<body onLoad="init('A sentence with "quoted text" as parameter')">

Unfortunately, this does work, as the quotes in the parameter are not treated properly.

Escaping the quotes also does not work

<body onLoad="init('A sentence with \"quoted text\" as parameter')">

(Above also does not work).

How do I deal with this. I though maybe I can create a string variable and assigne my sentence (with quotes) to it. But I dont know how to do it! The body onload is HTML and the Javascript variables would be visible only within the scope of the script, right? To be precise, the following does not work:

<script language="JavaScript">
var dada='A sentence with \"quoted text\" as parameter';
</script>
<body onLoad="init($dada, '</a>')">
Share Improve this question asked Oct 23, 2010 at 9:39 JP19JP19 1
  • 6 The language attribute is deprecated – Harmen Commented Oct 23, 2010 at 9:44
Add a comment  | 

4 Answers 4

Reset to default 10

You would have to use HTML entities to make it work:

<body onLoad="init('A sentence with &quot;quoted text&quot; as parameter')">

the much cleaner way, though, would be to assign the value in a separate <SCRIPT> part in the document's head.

...
<script>
body.onload = function() { init('A sentence with "quoted text" as parameter'); }
</script>
<body>
...

the onload event has the general downside that it is fired only when the document and all its assets (images, style sheets...) have been loaded. This is where the onDOMLoad event comes in: It fires when the core HTML structure is ready, and all elements are rendered. It is not uniformly supported across browsers, though, so all frameworks have their own implementation of it.

The jQuery version is called .ready().

Rather than inlining the onLoad method, why not set it programatically. You can then use the power of closures to get the data into the function.

<script type="text/javascript">
var data = "some data";
document.body.onload = function()
{
    alert(data);
}
</script>

not the nicest way

onload='init("A sentence with \"quoted text\" as parameter")'

By the way, I was making a silly mistake.

<script language="JavaScript"> 
var dada='A sentence with \"quoted text\" as parameter'; </script> 
<body onLoad="init(dada, '</a>')"> 

This works too. I was using $dada instead of dada in the onload call.

发布评论

评论列表(0)

  1. 暂无评论