error "The variable var is not defined." number -2753 from "var" in a method I'm trying to write a method that waits for an element on a webpage to appear before continuing, this code works when not placed in a method but I cannot appear to place it in one without getting the error above

on waitForId(idText)
set var to false
set idText to "\"" &amp; idText &amp; "\""
repeat until var is true
    tell application "Safari"
        set var to (do JavaScript "document.contains(document.getElementById(idText));") in current tab of first window
    end tell
    delay 0.2
end repeat
return true
end waitForId


tell application "Safari"
global var
set var to false
if not (exists document 1) then reopen
tell current tab of window 1 to set URL to "https://www.google.com/?client=safari#channel=mac_bm"
tell current tab to activate
log "starting wait"
my waitForId("hplogo")

log "done waiting"
end tell


The "hplogo" is a google sign for testing. When run:

error "The variable var is not defined." number -2753 from "var"
figured it out, a misplaced ) and concatenating the javascript to make it a variable, silly question.

on waitForId(idText)
set wait to false
set idText to "\"" &amp; idText &amp; "\""
repeat until wait is true
    tell application "Safari"
        set wait to (do JavaScript "document.contains(document.getElementById(" &amp; idText &amp; "));" in current tab of first window)
    end tell
    delay 0.2
end repeat
return true
end waitForId


tell application "Safari"
set var to false
if not (exists document 1) then reopen
tell current tab of window 1 to set URL to "https://www.google.com/?client=safari#channel=mac_bm"
tell current tab to activate
log "starting wait"
my waitForId("hplogo")

log "done waiting"
end tell