HTML: parameter in javascript function
Question: Can we put a string parameter in a JS function, while i'm in html? Like this:

    
    
    <form name="form3" action="mat.py" method="get" onsubmit="return validation(param1,param2)"/> 
    

I can also say that i'm working in Python, so my code is like that: there's
just two ' ', i don't think it can deal damages

    
    
     print'<form name="form3" action="mat.py" method="get" onsubmit="return validation(param1,param2)"/>'
    

I included my JS in an other file

Thank you, Clément.


Answer: There are _three layers_ to this statement:

    
    
    print'<form name="form3" action="mat.py" method="get" onsubmit="return validation(param1,param2)"/>'
    

There's

  1. The Python layer, the whole HTML thing is one big string in `'` quotes.

  2. The HTML layer, which is putting the attribute values in `"` quotes.

  3. The JavaScript layer, where you don't currently have any quotes.

It's important to keep track of what's happening at each level.

So, you have a couple of options:

  1. Outputting JavaScript code inside HTML attribuets is why JavaScript originally had two kinds of quotes (it now has three), both `"` and `'`. So you can use `\` in your Python code to put `'` around the params:
    
        print'<form name="form3" action="mat.py" method="get" onsubmit="return validation(\'param1\', \'param2\')"/>'
    

That outputs this:

    
        <form name="form3" action="mat.py" method="get" onsubmit="return validation('param1', 'param2')"/>'
    

Note we're using `'` around the JavaScript strings, so as not to end the HTML
attribute early.

It also works if we use `'` for the HTML attribute quotes and `"` in the
JavaScript, since HTML also allows both kinds of quotes:

    
        print'<form name=\'form3\' action=\'mat.py'\ method=\'get\' onsubmit=\'return validation("param1", "param2")\'/>'
    

which outputs

    
        <form name='form3' action='mat.py' method='get' onsubmit='return validation("param1", "param2")'/>
    

  2. Remember that the content of HTML attributes **is HTML** , so you can use `&quot;`, the named character entity for `"`:
    
        print'<form name="form3" action="mat.py" method="get" onsubmit="return validation(&quot;param1&quot;, &quot;param2&quot;)"/>'
    

which outputs

    
        <form name="form3" action="mat.py" method="get" onsubmit="return validation(&quot;param1&quot;, &quot;param2&quot;)"/>
    

Not very readable though. :-)

But your best option is _don't use`onxyz` attribute handlers at all_, use
modern techniques for hooking up events.

