如何从$(document).ready(function())调用javascript函数

时间:2012-05-04 00:53:03

标签: javascript jquery html

正如您在下面看到的,我试图在我的文档加载后调用这两个函数,但是当我运行代码时出现以下错误:syntax error unexpected token: <

编辑:我在下面添加了我的HTML代码,以便您仔细查看它。感谢

这是我的index.php文件:

<?php
require 'fb-php-sdk/facebook.php';
$app_id = '251156051648844';
$app_secret = 'be37135d238e66ddce8f4ce8db20e668';
$app_namespace = '';
$app_url = 'http://www.paulmeskers.com/rutgers/twenty/canvas/';
$scope = 'email,publish_actions';

// Init the Facebook SDK
$facebook = new Facebook(array(
'appId'  => $app_id,
'secret' => $app_secret,
));

// Get the current user
$user = $facebook->getUser();

// If the user has not installed the app, redirect them to the Auth Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
  'scope' => $scope,
  'redirect_uri' => $app_url,
));

print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
}

if(isset($_REQUEST['request_ids'])) {
  $requestIDs = explode(',' , $_REQUEST['request_ids']);
  foreach($requestIDs as $requestID) {
  try {
    $delete_success = $facebook->api('/' . $requestID, 'DELETE');
   } catch(FacebookAPIException $e) {
    error_log($e);
    }
  }
}



?>

<!DOCTYPE html>

<html>
<head>
<title>Twenty Questions</title>
<meta property="og:title" content="ttt" />
<meta property="og:type" content="activity" />
<meta property="og:url" content="https://apps.facebook.com/251156051648844/" />
</head>
<body id="yo">
<div id="fb-root"></div>
<script src="//connect.facebook.net/en_US/all.js"></script>
<script type='text/javascript' src='jquery-1.7.2.min.js'></script>
<script type='text/javascript' src='ui.js'></script>

<script>
    var appId = '<?php echo $facebook->getAppID() ?>';
    var uid;

    // Initialize the JS SDK
    FB.init({
        appId: appId,
        cookie: true,
    });

    FB.getLoginStatus(function(response){
        uid = response.authResponse.userID ? response.authResponse.userID: null;
    });

function authUser() {
    FB.login(function(response) {
    uid = response.authResponse.userID ? response.authResponse.userID : null;
    }, {scope:'email,publish_actions'});

}


</script>


    <input type="button" id="b1" value="Create Game" onClick="createGame(uid)"/>
<input type="button" id="b2" value="Share this game" onClick="shareOnTimeline()" />
    <input type="button" id="b4" value="fetch created games" onClick="fetch_creator_games(uid)" />
    <input type="button" id="b5" value="fetch joined games" onClick="fetch_joined_games()"/>
    <input type="button" id="b3" value="tewst" onClick="build_creator_ui()" />

    <p><b>Games created by me:</b></p>
    <div id="createdGames" ></div>
    <p>------------------------------------------------------</p>
    <p><b>Games created by others:</b></p>
    <div id="invitedGames" ></div>



</body>
</html>

2 个答案:

答案 0 :(得分:1)

在页面执行过程中的某个地方,<字符结束了它应该不存在的位置。由于您建议在jQuery源文件中引发此错误,因此可能是您的选择器编写错误。也许你试图选择一个HTML元素并忘记用引号括起来,或者你试图创建一个元素。

保持您的JavaScript清洁

可能会出现类似于以下内容的错误:

$(<input>).attr("id", "foo");

请注意,选择器不会用引号括起来,因此会引发一个类似于你收到的错误。

检查AJAX响应

我注意到你希望JSON能够进入管道,但是在将一些POSTS推送到你的服务器后,我发现一个不正确的用户ID会导致以下响应:

<br/><br/>
<label style='color:red;font-size:200%;'>
  Unable to load guessing games for user_id
</label>
<br/>

这可能会导致问题。请务必将您的错误消息也以JSON格式发送。

{ success: 0, message: 'Unable to load guessing games for this user id' }

要在PHP中构造此JSON字符串,您可以执行以下操作:

$response = array( 'success' => 0, 'message' => 'Unable to...this user id' );
echo json_encode( $response );

保持标记清洁

确保您有一个合适的页面:

<!DOCTYPE html><!-- no whitespace or output before the doctype -->
<html>
  <head>
    <title>Title Here</title>
    <style>
      body { color: red } /* styles in the head */
    </style>
    <script>var scripts_in_the_head_tag = true;</script>
  </head>
  <body>
    <p>HTML goes within the body tags.</p>
    <script>var scripts_in_the_body_tag = true;</script>
  </body>
</html>

答案 1 :(得分:0)

当我发出需要JSON响应的ajax请求时,我通常会看到此错误,但会收到HTML。