2

I have,

fruits = [ apple, banana, pineapple, oranges] 
htmlfile = open('home-page.html','w')
htmlfile.write("<html>\n")
htmlfile.write("<head>\n")
htmlfile.write("<title> Home </title>\n")
htmlfile.write("</head>\n")
htmlfile.write("<body>\n")
htmlfile.write("<h1> Fruit-properties </h1?\n")
htmlfile.write("<a href = " 'fruit+'.html"> '+fruit+'</a><br>\n")
htmlfile.write("</body>\n")
htmlfile.write("</html>\n")
htmlfile.write.close() 

I have separate code to create fruit.html for each fruit and can't write here because it is part of big code that I am writing.

so basically everything works fine. but when I click on any link, it is opened in the same browser. Is there any way that I can write in python to open the link in different tab??

I can't make use of webbrowser option of python, because it is a linked page to my home-page.

Can somebody help?

geek_xed
  • 165
  • 1
  • 8
  • 15
  • 1
    How has this got anything to do with Python? – jonrsharpe Jul 15 '15 at 15:33
  • And why did someone vote this question up? – Matthias Jul 17 '15 at 19:59
  • This question is related to python because html is implemented using it. And someone will vote it up because that person found it useful. Very much discouraging administrators you are @jobrsharpe. We look at stackoverflow as a good source of coding help. I certainly doubt what way you judge the posts. In one day you downvoted all my posts and now I can't post questions. Very much sad and discouraging this is. – geek_xed Jul 18 '15 at 01:48
  • Thanks people for voting up. But still I am not allowed to post my questions. – geek_xed Jul 18 '15 at 13:27
  • can somebody help me with this question? http://askubuntu.com/questions/651444/how-to-delete-duplicate-lines-starting-with-specific-word-in-python , I am unable to post on stack overflow – geek_xed Jul 22 '15 at 19:33

3 Answers3

1

If you add target="_blank" and modify your link tag to

'<a href="' + fruit + '.html" target="_blank">' + fruit + '</a><br>\n'

the link will open in a new browser or tab.

Edit: As pointed out the behaviour is browser-dependent, but it's the most you can do. As it seems there was a CSS3 proposal for this issue but it was abandoned (source).

Community
  • 1
  • 1
adrianus
  • 3,141
  • 1
  • 22
  • 41
  • it is actually opening it in separate window instead of tab – geek_xed Jul 15 '15 at 15:12
  • I guess that's browser-dependent; In my browser (Chrome) it opens up a new tab. – adrianus Jul 15 '15 at 15:14
  • will this work in IE? webbrowser.get(internet explorer).open(website url), it throws me an error like browser not found. any specific syntax for IE is used in python? – geek_xed Jul 15 '15 at 15:28
1

You can add the following property to your <a> tag to make the link open up in a separate tab or browser window - target="_blank" . So in your above code, it would become -

htmlfile.write('<a href = " 'fruit+'.html" target="_blank"> '+fruit+'</a><br>\n')

Also, the above line had some problems with quotes, you should open and close with single quotes, since you are using single quotes in the middle for fruit.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
0

I found that by default it was making use of some other browser to popup the home page. So in my code I made Mozilla-firefox as my default browser and solved the problem.

webbrowser.get('firefox').open(website) 

I tried the same for internet explorer too and it worked.

geek_xed
  • 165
  • 1
  • 8
  • 15