-2

We have a program which runs either on a live environment or on a test environment. The only difference is that the URL on the test environment contains "/test/". When in the test environment, some functions cause it to switch to the live environment (when the URL was hard coded!), so I'm trying to fix that.

I have created this function in JavaScript:

function GetTestMode()
{
    // If "/test/" exists in URL, keep it there (we are in test mode).
    var currentURL = document.URL;
    var sTestMode = "";
    if (currentURL.indexOf("/test/") != -1)
    {
    // test mode (according to URL)
      sTestMode = "test/";
    }
    return sTestMode;
}

This is used by the JavaScript functions and works as expected.

However, most of the UI code is in Python (creating HTML). So I want to get this JavaScript result in Python. So far I have:

import cgi
import os
import sys
import re

def DrawUI (self, short=False):
    print '<br />'
    print '<div class="box2">'
    print '<table cellspacing=0 cellpadding=0 border="0">'
    print '<tr>'

    sTestMode = "test mode = %s" % ('<a href="javascript:GetTestMode()" >')
    print 'sTestMode = ' + sTestMode + '<br />'

This causes the print statements following this code (which can even be print 'hello world') to become a link which, if I click on it, displays the correct return value from the JavaScript function in the browser.

How can I get sTestMode to contain the return value (without user interaction)?

  • Do you use a framework? How do the printed strings get to the browser? – Fabian Aug 14 '12 at 10:55
  • @Fabian: I am a complete newbie editing inherited code, so I'm not sure. I don't think we use a framework (just checking the imports). Other than what is stated here, I don't know how the printed strings get to the browser. We have a folder where we put the Python files and pages are immediately visible in our company intranet. – Reinstate Monica - Goodbye SE Aug 14 '12 at 11:00
  • Is it Apache with mod_python or mod_wsgi maybe? What modules does it import at the top of the file you are editing? Could you put them into your question? You should really figure out what you are using and let us know, so that we can respond properly. Anyway, what you need are either form submissions or Ajax calls via the XMLHttpRequest object. – Fabian Aug 14 '12 at 13:19
  • I was going to suggest the same thing using jQuery AJAX - http://ryanfunduk.com/web-dynamism-1/ – Aaron Newton Aug 14 '12 at 13:24
  • Thanks for the responses. I simply don't have the answers to those questions yet. But I have got a [solution](http://stackoverflow.com/questions/11935348/get-current-url-in-python-using-os-environ/11953640#11953640) to *my specific original problem* so I no longer need this "workaround". – Reinstate Monica - Goodbye SE Aug 14 '12 at 13:48
  • @Wikis but you can copy & paste the import statements at the top of your python script? That might give some hints what framework you use. – Fabian Aug 14 '12 at 14:13

1 Answers1

1

I assume you are using mod_python.

Here's a hacked up example how to get the Javascript variable to the server.

In Python, using the cgi module:

form = cgi.FieldStorage()
sTestMode = form.getfirst('sTestMode')
sTestMode = cgi.escape(sTestMode)  # Avoid script injection escaping the user input

In JavaScript, using JQuery (not sure if this is correct, maybe some JQuery guru can fix this if wrong):

var filename = '/url/to/your/script';
$.ajax({
  url: filename + '?sTestMode=' + sTestMode,
  type: 'GET'
});

If you have problem implementing the Javascript version you can make sure your Python script is correct by calling /url/to/your/script?sTestMode=something_funny in your browser.

However, you should read up the concepts of CGI, Ajax and how mod_python works.

Fabian
  • 4,160
  • 20
  • 32