0

I need to do some ugly stuff. But I cannot do it another way. what I need is

<a href='some/url/?with&params' onclick="(if confirm('Do you wanna submit?')
    {some code to submit form in href property})">  

but I don't know how to make inline script work... and I hesitate about the way of submitting it with window.location or document.location. Any ideas?

oleg.foreigner
  • 993
  • 2
  • 13
  • 28
  • Why do you need to do it like this? Writing code in a string is bound to pose issues eventually. It'll be hard to maintain and you won't have the benefit of validation with your IDE for potential syntax errors. – Aram Kocharyan Jan 25 '13 at 11:28
  • I need to add two links to submit form with the given url... I was sent onlu widjets file (django app)... I have no templates and form sources... – oleg.foreigner Jan 25 '13 at 11:31
  • The great thing about JavaScript is that it's easily injectable into other environments. Just add your own script and wait for the DOM to be ready, then use jQuery or other methods to identify that href (give it a class/id) and write all your event code in a callback with http://api.jquery.com/click/ – Aram Kocharyan Jan 25 '13 at 11:49

2 Answers2

1

Try this:

on html:

<a href='some/url/?with&params' onclick="if (confirm('Do you wanna submit?')) return MyValidation.actionSubmit();">Submit</a>

<script type="text/javascript" src="myValidation.js" />

myValidation.js

I used and on this answer.

; //starts with this to close other js not yet closed
var MyValidation = {

    actionSubmit : function() {

            //your code go here

            //example of calling another function
            MyValidation.anotherFunction();

            //example of accessing a global variable (for your validation)
            MyValidation.variable;

            //example of declaring local variable
            var word = "hello";

            //example of calling another function with parameters
            MyValidation.anotherFunctionWithParameters(word, MyValidation.variable);

            //the returning
            return MyValidation.someValidation(word);
    }

    //Yes, this is a comma. 
    //I'm putting in the beginning so we see this and didn't forget :)
    ,anotherFunction : function() {

            //some code
            //maybe a return
    }

    ,variable : {}

    ,anotherFunctionWithParameters : function(param1, param2) {

            //more code, maybe a return
    }

    ,someValidation : function(parameter) {
        //some validation
        return true|false;
    }

};

Take a look on this:

  1. How do JavaScript closures work?
Community
  • 1
  • 1
Rafael Perez
  • 162
  • 4
  • 14
  • I tried this... but it breaks the layout and I don't know why... maybe I did something wrong last time. – oleg.foreigner Jan 25 '13 at 11:35
  • Broken layout? Inline coding won't going to fix this. Post your html+css+js so we can help. – Rafael Perez Jan 25 '13 at 11:39
  • I found an error)) it was my fault)))) code you give works fine! but there is still a problem... how to submit a form via given link? I was proposed to use window.location but I'm soooo newbie in javascript... – oleg.foreigner Jan 25 '13 at 11:44
  • Well, this kind of question is simple. In cases like this, you should search first. I found this answer [here](http://stackoverflow.com/a/313624/1413158) that covers your question. There's a jQuery solution in the question itself; Try to use the function `get_form` by @hasen-j, you will learn more from it. – Rafael Perez Jan 25 '13 at 11:50
  • However, over time you will learn how the Javascript works then you will able to use APIs like [jQuery](http://api.jquery.com/). See [tag:jQuery]. – Rafael Perez Jan 25 '13 at 11:59
0

Have you heard about AJAX? Google it if you don't. The onClick parameter accepts a function name with or without parameters, not "inline scripts".

<script type=text/javascript>
  function doStuff(){
      if(confirm("Submit?"){
       //do your stuff with AJAX to some/url/?with&params

     }
  }
</script>
<a href='javascript:doStuff'>link</a>
  • I've heard but will it work the proper way if form doesn't return anything? – oleg.foreigner Jan 25 '13 at 11:46
  • You can pretty much have the complete code of jquery included in a onclick (don't try it :)) - It is not limited to only function names. Not saying you should put a lot of logic in onclick however, just clarifying. – hank Jan 25 '13 at 11:58