2

Hi guys This is my code of two javascript.i want to access variable defined in first javascript into another script.

1)

<script>
$(document).ready(function() 
{
    $('pre.codeguru').each(function() 
    {
            var pre = this;
            var form = $('form[name=sample]').clone();
            $(form).removeAttr('name');
            $(form).removeClass('hidden');
            $($(form).find('textarea')[0]).val($(pre).text());
            var id = $(pre).attr('id');
            $(form).find('div textarea[name=code]').first().attr('id', id);
        $(pre).replaceWith(form);
        });
        var editors = [];
        $('textarea[name=codeguru]').each(function() 
        {
            var editor = CodeMirror.fromTextArea(this, 
            {
                lineNumbers: true,
                matchBrackets: true,
                mode: "application/x-httpd-perl",
                tabMode: "shift"
             });
                editors.push(editor);
        });

});
</script>

2)

<script type="text/javascript">

    function execute() {
            p5pkg.CORE.print = function(List__) {
                var i;
                for (i = 0; i < List__.length; i++) {
                  document.getElementById('print-result').value += p5str(List__[i])
                }
                return true;
            };
            p5pkg["main"]["v_^O"] = "browser";
            p5pkg["main"]["Hash_INC"]["Perlito5/strict.pm"] = "Perlito5/strict.pm";
            p5pkg["main"]["Hash_INC"]["Perlito5/warnings.pm"] = "Perlito5/warnings.pm";
            var source = editor.getValue();
            alert(source);
            var pos = 0;
            var ast;
            var match;
            document.getElementById('print-result').value = "";
            try {
                var start = new Date().getTime();
                var js_source = p5pkg["Perlito5"].compile_p5_to_js([source]);
                var end = new Date().getTime();
                var time = end - start;
                // run
                start = new Date().getTime();
                eval(js_source);
                end = new Date().getTime();
                time = end - start;
            }
            catch(err) {
                //document.getElementById('log-result').value += "Error:\n";
                  }
        }
    </script>

Now my problem is i want to access the editor defined in first javascript as

var editors = [];
        $('textarea[name=codeguru]').each(function() 
        {
            var editor = CodeMirror.fromTextArea(this, 
            {
                lineNumbers: true,
                matchBrackets: true,
                mode: "application/x-httpd-perl",
                tabMode: "shift"
             });
                editors.push(editor);
        });

in second javascript. anyone has answer of this then please help me to do so

3
  • 2
    declare it as a global variable and access it in entire page Commented Jul 9, 2013 at 8:04
  • why don't you make editors in first file global by just writing var editors=[] next to script tag Commented Jul 9, 2013 at 8:04
  • i have used global variable.it worked but the new problemis arised is i cant retrieve the data from the editor Commented Jul 9, 2013 at 8:46

3 Answers 3

2

If you leave out var while defining variables they will be globally accessible.

So

pre = this;

instead of

var pre = this;

would make pre accessible from every function.

Sign up to request clarification or add additional context in comments.

1 Comment

i have used global variable.it worked but the new problemis arised is i cant retrieve the data from the editor
0

the only way I can think is to pass the variable into the other functions as a variable

function otherJavaFile.myFunction (myVariable);

or alter a variable in the HTML i.e. the custom data-value and then the other script can access it. I don't like global variables.

// Sheet 1
$("#myDiv").attr("data-variable",yourValue);

// Sheet 2
 var secondVariable = $("#myDiv").attr("data-variable");

Comments

0

buddy i am not comfortable with jquery...
I hope you are looking forward for the iframes/frames on same document[window sharing]. Based on my knowledge of Javascript DOM to access a variable defined in one document inside another document.You have to use document.importNode(original Node as in other document,boolean) method as per DOM 2.
Do something like this for javacript code ...
documentI(original variable/node present here)- iframe.contentDocument.getElementsByTagName(/Tag name of Node/)...
documentII(node to be cloned here)- document.importNode(originalNode,True)

I hope this works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.