Javascript API用户页面编码风格

Javascript API user page coding style

本文关键字:编码 风格 API 用户 Javascript      更新时间:2023-09-26

我有包含Jsonp API函数的Javascript源代码。无论在哪里打印Javascript源代码,我都要先检查api标签是否;如果已经打印出来了->执行API函数,如果"API"标签;如果还没有打印,则等待标签打印到屏幕上,然后执行API函数。

例如,我有这样的代码:
<html>
  <head>
    <!-- js code - contain Jsonp functions -->
    <script type="text/javascript" src="jsonp.js"></script>
  </head>
  <body>
    <!-- when this newtag get print , the function starts -->
    <newtag:api size="small">myNewTag</newtag:api>
  </body>
</html>

我不知道用户会决定把JS代码放在哪里,它可能在头部,身体或在页面中间,因为我有这个神秘我不能做这样的事情:

<newtag:api size="small">myNewTag</newtag:api>
<script> startJsonp(); </script>

因为如果js代码还没有打印,订单将不会被执行。

  • 如果代码在头部打印,它将无法工作,因为"api标签"

  • 要使API工作,我需要" API标签"要在屏幕上打印,Jsonp函数才能工作

我能做什么来解决这个问题?

你能等到窗口加载完成吗?

<body onload='startJsonp();'>